在滚动时,CardView之间的差距会增加

时间:2016-04-21 23:47:03

标签: android android-layout android-recyclerview android-cardview

我使用带有RecyclerView的CarViews并且它在加载时看起来很好但是当滚动列表时,CardViews之间的间隙增加,并且一次只显示视图中的一张卡。

这是我的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>

以下是我使用该视图的方式

    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);
        }
    }
}

这是滚动列表之前的屏幕截图 enter image description here

滚动后

enter image description here

提前致谢。

1 个答案:

答案 0 :(得分:5)

根布局使用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">