我正在尝试为要进入学校应用程序的清单创建卡片视图。 ArrayList中的所有项目都进入一张卡,我不知道为什么。我看了所有可以找到的教程,但是没有运气。
这是相关的代码。任何帮助都将不胜感激。

活动

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



}


RecycleView 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类没有与分隔符相关的参数。相反,您需要扩展RecyclerView的内部类ItemDecoration


  ItemDecoration允许应用程序添加特殊图形并
  适配器数据集中特定项目视图的布局偏移量。这个
  可用于在项目,高光,视觉之间绘制分隔线
  分组边界等等。
  
  所有ItemDecorations均按添加顺序绘制,
  项目视图(在onDraw()中和项目之后(在onDrawOver(Canvas, RecyclerView, RecyclerView.State)中)。


垂直间距ItemDecoration
扩展ItemDecoration,添加将空格高度作为参数的自定义构造函数,并覆盖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;
    }
}


如果不想在最后一项下方插入空格,请添加以下条件:

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


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

分频器项装饰
扩展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的构造函数,例如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()

用法

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

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的图书馆,应该可以简化物品装饰过程。还没有尝试过。

其功能包括:

库存物品装饰的集合,包括:


项目间距水平/垂直分隔线。
项目清单

08-28 21:59