重点 (Top highlight)
ConcatAdapter
is a new class available in recyclerview:1.2.0-alpha02
which enables you to sequentially combine multiple adapters
to be displayed in a single RecyclerView
. This enables you to better encapsulate your adapters rather than having to combine many data sources into a single adapter, keeping them focused and re-usable.
ConcatAdapter
是recyclerview:1.2.0-alpha02
可用的新类,它使您可以顺序组合多个adapters
,以在单个RecyclerView
显示。 这使您可以更好地封装适配器,而不必将许多数据源组合到单个适配器中,从而使它们集中并可重复使用。
One use case for this is displaying a list loading state in a header or footer: when the list is retrieving data from the network, we want to show a progress spinner; in case of error, we want to show the error and a retry button.
一个用例是在页眉或页脚中显示列表加载状态:当列表从网络中检索数据时,我们想显示一个进度条。 如果出现错误,我们要显示错误和重试按钮。

介绍Concat Adapter
(Introducing ConcatAdapter
)
ConcatAdapter
allows us to display the contents of multiple adapters, in a sequence. For example, let’s say that we have the following 3 adapters:
ConcatAdapter
允许我们按顺序显示多个适配器的内容。 例如,假设我们有以下3个适配器:
val firstAdapter: FirstAdapter = …val secondAdapter: SecondAdapter = …val thirdAdapter: ThirdAdapter = …val concatAdapter = ConcatAdapter(firstAdapter, secondAdapter, thirdAdapter)recyclerView.adapter = concatAdapter
The recyclerView
will display the items from each adapter sequentially.
recyclerView
将顺序显示每个适配器中的项目。
Having different adapters allows you to better separate the concerns of each sequential part of a list. For example, if you want to display a header, you don’t need to put the logic related to the header display in the same adapter that handles the list display, rather you can encapsulate it in its own adapter.
使用不同的适配器可以使您更好地分离列表的每个顺序部分。 例如,如果要显示标题,则不需要将与标题显示相关的逻辑放在处理列表显示的同一适配器中,而是可以将其封装在其自己的适配器中。

在页眉和页脚中显示加载状态 (Displaying load state in a header and footer)
Our header/footer displays either a progress indicator or reports an error. When the list has successfully finished loading, the header/footer shouldn’t display anything. Therefore they can be represented as a list with 0 or 1 items, with their own adapter:
我们的页眉/页脚显示进度指示器或报告错误。 列表成功完成加载后,页眉/页脚不应显示任何内容。 因此,可以使用自己的适配器将它们表示为包含0或1个项目的列表:
val concatAdapter = ConcatAdapter(headerAdapter, listAdapter, footerAdapter)recyclerView.adapter = concatAdapter
If both the header and the footer use the same layout, ViewHolder
and UI logic (e.g when progress is displayed and how), you can implement just one Adapter
class and create 2 instances of it: one for the header and one for the footer.
如果页眉和页脚使用相同的布局, ViewHolder
和UI逻辑(例如,显示进度以及显示方式),则可以仅实现一个Adapter
类并创建2个实例:一个用于页眉,一个用于页脚。
For a complete implementation, check out this pull request, which adds:
要获得完整的实现,请查看此pull request ,其中添加了:
- A load state header and footer layout 加载状态页眉和页脚布局
A
ViewHolder
object for the header and footer页眉和页脚的
ViewHolder
对象A
ListAdapter
that displays 0 or 1 items based on theLoadState
. Every time theLoadState
changes, we notify that the item needs to be changed, inserted or removed (see code).一个
ListAdapter
,基于LoadState
显示0或1个项目。 每次LoadState
更改时,我们都会通知您需要更改,插入或删除该项目(请参见代码 )。