ANDROID: view multiple cards in recycler view

Please see below image:

How can I insert several kinds of cards into the Recycler view. or any other way to achieve this.
Using a Recycler view is necessary.

enter image description here

+4
source share
4 answers

I think the right way to achieve the goal described in the attached image would be GridLayoutManagerto use RecyclerView.LayoutManageror instead of using LinearLayoutManager.

LayoutManagerthat we attach to RecyclerViewdetermines the number of columns. There are 3 subclasses.

  • LinearLayoutManager
  • GridLayoutmanger
  • StaggeredGridLayoutmanger

, RecyclerView.LayoutManager,

RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManger(this);

GridLayoutManager mLayoutManager = new GridLayoutManger(this, 2);

2 . , , , 2 .

+6

    <?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>
+3

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>
+2

. . / .

    // 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));
       }
       ....
    }
0

Source: https://habr.com/ru/post/1606689/


All Articles