设计一个具有不同布局的 RecyclerView

Design a RecyclerView with different layouts

我有一个 RecyclerView 有不同的 CardView 布局。

我的问题是,如果我想在每个CardView上添加动态列表,那么我应该在每个卡上添加ListView还是在每个卡上添加RecyclerView

一个例子将不胜感激。

在 .onCreateViewHolder() 方法中您收到 int 变量 viewType 用它来区分不同的布局类型

老实说,将 RecyclerView 添加到另一个 RecyclerView 中并不常见。最好使用单个 RecyclerView 并动态填充列表项。这里有一个 github 项目来描述如何做到这一点 https://github.com/comeondude/dynamic-recyclerview/wiki。 我会说看看。可能有不同的方法,但是,这是一种在 RecyclerView 中显示多个列表的更快、更有效的方法。

The idea is to add logic in your onCreateViewHolder and onBindViewHolder method so that you can inflate proper view for the exact positions in your RecyclerView.

There is a sample project along with that wiki too. You might clone and check what it does.

There is another way of keeping your items in a single ArrayList of objects so that you can set an attribute tagging the items to indicate which item is from first list and which one belongs to second list. Then pass that ArrayList into your RecyclerView and then implement the logic inside adapter to populate them dynamically.

告诉我这是否解决了您的问题 ;)