ANDROID:回收者视图中的多个卡片视图

ANDROID: Multiple Card Views inside recycler view

请看下图:

如何在 Recycler 视图中插入多个卡片视图。 或任何其他方式来实现这一目标。
必须使用 Recycler 视图。

您的 xml 可以这样做:

<LinearLayout 
            android:layout_width="match_parent" 
            android:layout_height="wrap_content"
            android:orientation="horizontal">
        <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content">
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
                android:layout_width="0dp"
                android:layout_weight="1"
                android:layout_height="wrap_content">
        </android.support.v7.widget.CardView>
</LinearLayout>

试试这个

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="8dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <android.support.v7.widget.CardView

            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="300dp"
            android:orientation="vertical">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:src="@drawable/a"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Huming Bird"/>
        </android.support.v7.widget.CardView>
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_weight="1"
            android:layout_height="wrap_content">
            <ImageView
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:src="@drawable/b"/>
            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="Huming Bird"/>
        </android.support.v7.widget.CardView>
    </LinearLayout>

</LinearLayout>

我认为实现所附图片中描述的目标的正确方法是使用 GridLayoutManager 而不是使用 RecyclerView.LayoutManagerLinearLayoutManager

我们附加在RecyclerView上的LayoutManager决定了列数。大约有 3 个子类。

  1. LinearLayoutManager
  2. GridLayoutmanger
  3. StaggeredGridLayoutmanger

在初始化 RecyclerView.LayoutManager 的 activity 中,更改

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManger(this);

GridLayoutManager mLayoutManager = new GridLayoutManger(this, 2);

2 是网格的跨度数。每个项目都将放置在一个跨度中,因此您的回收站视图中将有 2 列。

要根据需要的列数动态设置,可以根据列数设置布局管理器。这是非常灵活的。具有相应布局的 same/similar 代码可以 运行 在任一平板电脑上,或 phone.

    // Set the adapter
    if (view instanceof RecyclerView) {
       Context context = view.getContext();
       RecyclerView recyclerView = (RecyclerView) view;
       if (mColumnCount <= 1) {
          recyclerView.setLayoutManager(new LinearLayoutManager(context));
       } else {
          recyclerView.setLayoutManager(new GridLayoutManager(context, mColumnCount));
       }
       ....
    }