Gap between CardViews increases on scrolling

2019-08-13 18:51发布

问题:

I'm using CarViews with RecyclerView and it looks fine when loaded but once when the list is scrolled the gap between the CardViews increases and there shows only one card in the view at a time.

Here is my CardView.xml

<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.support.v7.widget.CardView
        android:id="@+id/conversationCard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:padding="16dp">

            <TextView
                android:id="@+id/sender"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content" />

            <TextView
                android:id="@+id/abstractConvo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="35dp"/>

        </RelativeLayout>

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

</LinearLayout>

and here is how I'm using that view

    public class ConversationsListAdapter extends RecyclerView.Adapter<ConversationsListAdapter.ConversationsListViewHolder> {

    List<Conversation> conversationList;

    public ConversationsListAdapter(List<Conversation> conversationList) {
        this.conversationList = conversationList;
    }

    @Override
    public int getItemCount() {
        return conversationList.size();
    }

    @Override
    public ConversationsListViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.conversation_card, viewGroup, false);
        ConversationsListViewHolder listViewHolder = new ConversationsListViewHolder(view);
        return  listViewHolder;
    }

    @Override
    public void onBindViewHolder(ConversationsListViewHolder holder, int position) {
        holder.sender.setText(conversationList.get(position).getSenderPhNo());
        holder.abstractConvo.setText(conversationList.get(position).getAbstractConvo());
    }

    public static class ConversationsListViewHolder extends RecyclerView.ViewHolder {
        CardView cv;
        TextView abstractConvo, sender;

        public ConversationsListViewHolder(View itemView) {
            super(itemView);
            cv = (CardView) itemView.findViewById(R.id.conversationCard);
            sender = (TextView) itemView.findViewById(R.id.sender);
            abstractConvo = (TextView) itemView.findViewById(R.id.abstractConvo);
        }
    }
}

Here is the screenshot before the list is scrolled

and after scrolling

Thanks in advance.

回答1:

the root layout use wrap_content

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto">