使用mergeadapter顺序合并适配器

本文聚焦于如何使用MergeAdapter按顺序合并多个适配器,主要来源于Android Developers平台的一篇文章翻译。
摘要由CSDN通过智能技术生成
重点 (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.

ConcatAdapterrecyclerview: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.

一个用例是在页眉或页脚中显示列表加载状态:当列表从网络中检索数据时,我们想显示一个进度条。 如果出现错误,我们要显示错误和重试按钮。

Image for post
A RecyclerView with a footer displaying the loading state: progress or error
一个带有页脚的RecyclerView,显示加载状态:进度或错误

介绍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.

使用不同的适配器可以使您更好地分离列表的每个顺序部分。 例如,如果要显示标题,则不需要将与标题显示相关的逻辑放在处理列表显示的同一适配器中,而是可以将其封装在其自己的适配器中。

Image for post
RecyclerView and Adapter
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值