Gap between CardViews increases on scrolling

心不动则不痛 提交于 2019-12-11 05:55:58

问题


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">


来源:https://stackoverflow.com/questions/36782333/gap-between-cardviews-increases-on-scrolling

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!