Cardview全都进入一张卡片

时间:2016-02-08 03:46:06

标签: android android-recyclerview android-cardview

我正在尝试为将要进入学校应用的核对清单创建卡片视图。 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>

再次非常感谢你!

2 个答案:

答案 0 :(得分:1)

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

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

  

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属性以获得所需的效果。

Divider 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属性的构造函数,例如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()

用法

要使用您的新类,请将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的图书馆,它可以简化项目装饰过程。但是哈文没试过。

其功能包括:

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

  • 项目间距水平/垂直分隔线。
  • 列表项

答案 1 :(得分:0)

您只需要使用linearLayout封装您的cardview,并为linearLayout(内部或外部)设置填充。但不知何故,即使我分开了,我也没有阴影,所以我在外部线性布局中添加了边距。现在它看起来很好,但很奇怪,因为即使在官方文档中,也没有关于其他参数的信息,无论如何它都可以正常工作。