Cardview 全部进入一张卡片

Cardview is all going into one card

我正在尝试为将进入学校应用程序的清单创建卡片视图。 ArrayList 中的所有项目都进入一张卡片,我无法弄清楚为什么。我已经看过我能找到的每一个教程,但没有运气。 这是相关代码。非常感谢任何帮助。

Activity

recyclerView = (RecyclerView) findViewById(R.id.masterListRecView);
    recyclerView.setHasFixedSize(true);

layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());

theList = new ArrayList<MasterListItem>();

theList.add(new MasterListItem("Test Item1",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item2",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item3",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item4",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item5",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item6",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item7",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item8",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item9",new Date(),new Date(),"here are some notes",false));
theList.add(new MasterListItem("Test Item10",new Date(),new Date(),"here are some notes",false));

masterListAdapter = new RecViewAdapter(theList);
recyclerView.setAdapter(masterListAdapter);

masterListAdapter.notifyDataSetChanged();

RecycleView 适配器

public class RecViewAdapter extends RecyclerView.Adapter<RecViewAdapter.MyViewHolder> {
    TextView title;
    TextView dueDate;
    TextView completedDate;
    TextView notes;
    CheckBox completed;
    private ArrayList<MasterListItem> theList;
    //private LayoutInflater inflater;

    public RecViewAdapter(ArrayList<MasterListItem> theList) {
        this.theList = theList;
        //inflater = LayoutInflater.from(context);
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.master_list_item,parent,false);

        MyViewHolder myViewHolder = new MyViewHolder(view);
        return myViewHolder;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        title = holder.title;
        dueDate = holder.dueDate;
        completedDate = holder.completedDate;
        notes = holder.notes;
        completed = holder.completed;

        title.setText(theList.get(position).getTitle());
        dueDate.setText(theList.get(position).getDueDate().toString());
        completedDate.setText(theList.get(position).getCompletedDate().toString());
        notes.setText(theList.get(position).getNotes());
        completed.setChecked(theList.get(position).isCompleted());
    }



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

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
    }


    public class MyViewHolder extends RecyclerView.ViewHolder{
        private TextView title;
        private TextView dueDate;
        private TextView completedDate;
        private TextView notes;
        private CheckBox completed;
        public MyViewHolder(View itemView){
            super(itemView);
            this.title = (TextView) itemView.findViewById(R.id.masterListTitle);
            this.dueDate = (TextView) itemView.findViewById(R.id.masterListDueDate);
            this.completedDate = (TextView) itemView.findViewById(R.id.masterListCompletedDate);
            this.notes = (TextView) itemView.findViewById(R.id.masterListNotes);
            this.completed = (CheckBox) itemView.findViewById(R.id.masterListCompleted);
        }
    }



}

回收视图XML

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="edu.uco.weddingcrashers.hitched.MasterWeddingList">

    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+"
        android:id="@+id/addTaskButton"
        android:layout_alignParentTop="true"
        android:layout_alignParentEnd="true" />

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



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


</RelativeLayout>

卡片XML

    <?xml version="1.0" encoding="utf-8"?>


<android.support.v7.widget.CardView
    android:id="@+id/masterListCardView"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:padding="16dp"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    card_view:cardCornerRadius="4dp"
    xmlns:android="http://schemas.android.com/apk/res/android">


    <!--
    android:layout_alignParentStart="true"
        android:layout_gravity="center"
    android:layout_below="@+id/addTaskButton"
    -->


        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:orientation="vertical" android:layout_width="match_parent"
            android:layout_height="match_parent">


            <TextView
                android:id="@+id/masterListTitle"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="test title" />
            <TextView
                android:id="@+id/masterListDueDate"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="test title" />
            <TextView
                android:id="@+id/masterListCompletedDate"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="test title" />
            <TextView
                android:id="@+id/masterListNotes"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:text="test title" />
            <CheckBox
                android:id="@+id/masterListCompleted"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Completed"
                android:editable="false"
                android:layout_gravity="center_horizontal" />
        </LinearLayout>


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

再次感谢您!

您必须在 RecyclerView 项之间添加分隔符。给 RecyclerView 添加分隔符和装饰,与其他列表视图不同。你可以通过这个答案来实现:

与 ListView 不同,RecyclerView class 没有与分隔符相关的参数。相反,您需要扩展 ItemDecoration,一个 RecyclerView 的内部 class:

An ItemDecoration allows the application to add a special drawing and layout offset to specific item views from the adapter's data set. This can be useful for drawing dividers between items, highlights, visual grouping boundaries and more.

All ItemDecorations are drawn in the order they were added, before the item views (in onDraw() and after the items (in onDrawOver(Canvas, RecyclerView, RecyclerView.State).

垂直间距ItemDecoration 扩展 ItemDecoration,添加以 space 高度作为参数的自定义构造函数并覆盖 getItemOffsets():

public class VerticalSpaceItemDecoration extends RecyclerView.ItemDecoration {

    private final int mVerticalSpaceHeight;

    public VerticalSpaceItemDecoration(int mVerticalSpaceHeight) {
        this.mVerticalSpaceHeight = mVerticalSpaceHeight;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent,
            RecyclerView.State state) {
        outRect.bottom = mVerticalSpaceHeight;
    }
}

如果您不想在最后一项下面插入space,请添加以下条件:

if (parent.getChildAdapterPosition(view) !=
                              parent.getAdapter().getItemCount() - 1) {
            outRect.bottom = mVerticalSpaceHeight;
}

注意:您还可以修改 outRect.top、outRect.left 和 outRect.right 属性以获得所需效果。

分隔线 ItemDecoration 扩展 ItemDecoration 并覆盖 onDraw():

public class DividerItemDecoration extends RecyclerView.ItemDecoration {
    private static final int[] ATTRS = new int[]{android.R.attr.listDivider};

    private Drawable mDivider;

    /**
     * Default divider will be used
     */
    public DividerItemDecoration(Context context) {
        final TypedArray styledAttributes = context.obtainStyledAttributes(ATTRS);
        mDivider = styledAttributes.getDrawable(0);
        styledAttributes.recycle();
    }

    /**
     * Custom divider will be used
     */
    public DividerItemDecoration(Context context, int resId) {
        mDivider = ContextCompat.getDrawable(context, resId);
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();

        int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            View child = parent.getChildAt(i);

            RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

            int top = child.getBottom() + params.bottomMargin;
            int bottom = top + mDivider.getIntrinsicHeight();

            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
        }
    }
}

您可以调用使用默认 Android 分隔符属性的第一个构造函数,或者调用使用您自己的可绘制对象的第二个构造函数,例如 drawable/divider.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
       android:shape="rectangle">
    <size
            android:height="1dp" />
    <solid android:color="#ff992900" />
</shape>

注意:如果您希望在项目上绘制分隔线,请改写 onDrawOver()

用法

要使用新的 class,请将 VerticalSpaceItemDecoration 或 DividerSpaceItemDecoration 添加到 RecyclerView,例如在片段的 onCreateView():

private static final int VERTICAL_ITEM_SPACE = 48;
private RecyclerView mUiRecyclerView;
private LinearLayoutManager mLinearLayoutManager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_feed, container, false);

    mUiRecyclerView = (RecyclerView) rootView.findViewById(R.id.fragment_home_recycler_view);
    mLinearLayoutManager = new LinearLayoutManager(getActivity());
    mUiRecyclerView.setLayoutManager(mLinearLayoutManager);

    //add ItemDecoration
    mUiRecyclerView.addItemDecoration(new VerticalSpaceItemDecoration(VERTICAL_ITEM_SPACE));
    //or
    mUiRecyclerView.addItemDecoration(
            new DividerItemDecoration(getActivity());
    //or
    mUiRecyclerView.addItemDecoration(
            new DividerItemDecoration(getActivity(), R.drawable.divider));

    mUiRecyclerView.setAdapter(...);

    return rootView;
}

还有 Lucas Rocha 的库,它应该可以简化项目装饰过程。不过还没试过。

其特点包括:

一组库存物品装饰品,包括:

  • 项目间距 Horizontal/vertical 分隔线。
  • 列表项

您只需要用 linearLayout 包围您的 cardview 并为您的 linearLayout ( inner 或 outer )设置一个填充。但不知何故,即使我将它分开,我周围也没有阴影,所以我在外部 linearLayout 中添加了边距。现在看起来不错,但真的很奇怪,因为即使在官方文档中,也没有任何关于附加参数的内容,但它仍然可以正常工作。