简体   繁体   English

一个CardView中的多个RecyclerView项目

[英]Multiple RecyclerView items in one CardView

I'm trying to create multiple RecyclerView items in each CardView item as this app does: 我正在尝试在每个CardView项目中创建多个RecyclerView项目,因为这个应用程序:

在此输入图像描述

I had created a cardview_item.xml that contains the dataset item (the match in this example) 我创建了一个包含数据集项的cardview_item.xml(本例中的匹配项)

and a cardview_title.xml that contains the title of the card (the league in this example). 以及包含卡片标题的cardview_title.xml(本例中为联盟)。

cardview_title.xml: 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: 我扩展的RecyclerView.Adapter在单独的cardview中显示每个数据集项:

在此输入图像描述

How can I implement this with RecyclerView, CardView and ViewHolder design pattern? 如何使用RecyclerView,CardView和ViewHolder设计模式实现这一点?

Any help will be appreciated. 任何帮助将不胜感激。

Thanks 谢谢

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. 对于每个项目具有不同类型的布局或动态项目数量的RecyclerView将存在性能问题,因为我们无法使用ViewHolder模式重用视图。

Option 1: Use a recycler item layout with title and a dynamic layout which will inflate the given match details. 选项1:使用带有标题和动态布局的回收商项目布局,这将夸大给定的匹配详细信息。 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. 选项2:如果您可以决定联赛的最大匹配数,则定义具有最大匹配数的卡,并根据可用的匹配数据隐藏匹配。 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. 选项3:组合选项1和2.使用静态匹配项和动态项。 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. 这样您就可以帮助RecyclerView重用视图。

Hope it helps you. 希望它能帮到你。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM