我正在尝试像这个应用一样在每个 CardView 项目中创建多个 RecyclerView 项目:
我创建了一个包含数据集项(本例中的匹配项)的 cardview_item.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>
我扩展的 RecyclerView.Adapter 在单独的卡片 View 中显示每个数据集项:
如何使用 RecyclerView、CardView 和 ViewHolder 设计模式实现这一点?
我们将不胜感激。
谢谢
最佳答案
我最近实现了一个类似的模式。
具有不同类型布局或每个项目的动态项目数的 RecyclerView 将出现性能问题,因为我们无法使用 ViewHolder 模式重用 View 。
选项 1:使用带标题的回收站项目布局和动态布局,这将增加给定的匹配详细信息。但动态布局无法重复使用,因此您在滚动时会出现明显的延迟。
选项 2:如果您可以决定联赛的最大比赛数,则定义一张具有最大比赛数的卡片,并根据可用的比赛数据隐藏比赛。当您将它与单个动态布局进行比较时,这有助于我提高性能。
选项 3:结合选项 1 和 2。使用静态匹配项和动态项。仅在需要时使用动态布局。这将最大限度地减少动态布局将要膨胀的次数。这样您就可以帮助 RecyclerView 重用 View 。
希望对你有帮助。
关于android - 一个 CardView 中的多个 RecyclerView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30151899/