cardview recycleview android应用程序中的额外空间

时间:2019-03-04 12:10:54

标签: android cardview

我希望我的卡可以彼此对齐,但是它们会产生额外的空间,我不知道该如何解决。 我以为是recycleView的问题。

//My RVadapter
public class RVAdapter extends RecyclerView.Adapter<RVAdapter.PersonViewHolder>{
        List<Bus> abstractlistbus;

        public static class PersonViewHolder extends RecyclerView.ViewHolder {
            CardView cv;

            TextView bus_name;
            TextView controller_Status;
            Context mContext;


            PersonViewHolder(final View itemView, final Context context) {
              super(itemView);


                cv = (CardView)itemView.findViewById(R.id.cv);
                mContext=context;


               bus_name = (TextView)itemView.findViewById(R.id.name);
              controller_Status = (TextView)itemView.findViewById(R.id.Status);
     }

        }

        RVAdapter(List<Bus> abstractlist){
            this.abstractlistbus = abstractlist;

        }
        @Override
        public int getItemCount() {
            return abstractlistbus.size();
        }
        @Override
        public PersonViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
            View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.cards_layout, viewGroup, false);
            PersonViewHolder pvh = new PersonViewHolder(v,v.getContext());

            return pvh;
             }

        @Override
        public void onBindViewHolder(/*final*/ PersonViewHolder personViewHolder, final int i) {
           personViewHolder.bus_name.setText("Номер автобуса:"+abstractlistbus.get(i).number);
            personViewHolder.controller_Status.setText(abstractlistbus.get(i).controller+"");

    }
        }


//My activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"

    tools:context=".MainActivity"

    >



    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:scrollbars="vertical"
        />




</RelativeLayout>

//My cards_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:tag="cards main container">

    <android.support.v7.widget.CardView
        android:id="@+id/cv"
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"

        card_view:cardCornerRadius="10dp"
        card_view:cardElevation="5dp"
        card_view:cardUseCompatPadding="true">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            >



            <LinearLayout
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginTop="12dp"
                android:layout_weight="2"
                android:orientation="vertical"
                >

                <TextView
                    android:id="@+id/name"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10dp"
                    android:text="Android Name"
                    android:textAppearance="?android:attr/textAppearanceLarge" />

                <TextView
                    android:id="@+id/Status"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center_horizontal"
                    android:layout_marginTop="10dp"

                    android:text="Android Version"
                    android:textAppearance="?android:attr/textAppearanceMedium"/>

            </LinearLayout>
        </LinearLayout>

    </android.support.v7.widget.CardView>




</LinearLayout>


//And piece of code im MainActivity.java
 RecyclerView rv = (RecyclerView) findViewById(R.id.my_recycler_view);
            rv.setHasFixedSize(true);


            LinearLayoutManager llm = new LinearLayoutManager(this);
            rv.setLayoutManager(llm);

extra space between cards

enter image description here enter image description here

enter image description here

4 个答案:

答案 0 :(得分:1)

in your card linear layout make **android:layout_height="match_parent"** to **android:layout_height="wrap_content"**

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:tag="cards main container"> 

答案 1 :(得分:0)

在适配器视图中,将CardView的高度match_parent更改为wrap_content

答案 2 :(得分:0)

在您的cards_layout.xml中-将这些边距添加到线性布局中。

android:layout_marginTop="5dp" 
android:layout_marginBottom="5dp"

这将解决您的问题。

答案 3 :(得分:0)

在您的CardView中尝试使用此标签:

        app:cardUseCompatPadding="true"