Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Multiple RecyclerView items in one CardView

I'm trying to create multiple RecyclerView items in each CardView item as this app does:

enter image description here

I had created a cardview_item.xml that contains the dataset item (the match in this example)

and a cardview_title.xml that contains the title of the card (the league in this example).

cardview_title.xml:

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="88dp"
card_view:cardCornerRadius="1dp"
android:layout_margin="4dp">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TextView
        android:id="@+id/amount_title"
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:layout_marginEnd="5dp"
        android:layout_marginTop="5dp"
        android:gravity="center"
        android:layout_alignParentEnd="true"
        android:padding="5dp"
        android:text="@string/amount"
        android:textAllCaps="true"
        android:textSize="20sp"
        android:textColor="@android:color/black"/>

    .
    .
    .

    <TextView
        android:id="@+id/week_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@id/amount_title"
        android:layout_alignTop="@id/amount_title"
        android:layout_alignParentStart="true"
        android:layout_marginStart="5dp"
        android:gravity="center"
        android:padding="5dp"
        android:textAllCaps="true"
        android:text="@string/week"
        android:textColor="@android:color/black"
        android:textSize="20sp" />

    <View
        android:id="@+id/lineSeparator"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:layout_below="@id/week_title"
        android:background="@android:color/darker_gray"/>

    <include
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        layout="@layout/cardview_item"
        android:layout_below="@+id/week_title"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

The RecyclerView.Adapter that I extended shows each dataset item in separate cardview:

enter image description here

How can I implement this with RecyclerView, CardView and ViewHolder design pattern?

Any help will be appreciated.

Thanks

like image 238
Moshik L Avatar asked Nov 09 '22 13:11

Moshik L


1 Answers

I recently implemented a similar pattern.

A RecyclerView which has different type of layouts or dynamic number of items for each item will have performance issues as we cannot reuse the views with ViewHolder pattern.

Option 1: Use a recycler item layout with title and a dynamic layout which will inflate the given match details. But the dynamic layout cannot be reused and so you will get visible lags while you scroll.

Option 2: If you can decide the maximum number of matches for a league then define a card with maximum number of matches and hide the matches as per the available match data. This helped me to improve the performance when you compare it with the single dynamic layout.

Option 3: Combine option 1 and 2. Use static match items along with a dynamic item. Utilise dynamic layout only when needed. This will minimise the number of times the dynamic layout is going to be inflated. So that you can help the RecyclerView in reusing the views.

Hope it helps you.

like image 131
balachandarkm Avatar answered Nov 14 '22 22:11

balachandarkm