前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >Android新发布的ConcatAdapter使用教程

Android新发布的ConcatAdapter使用教程

作者头像
Rouse
发布2021-05-28 17:39:09
1.8K0
发布2021-05-28 17:39:09
举报
文章被收录于专栏:Android补给站Android补给站

前言

2021年4月7日Android团队正式发布了RecyclerView 1.2.0版本。相对于1.1.0版本,它有两个主要的变化:

  1. 增加了ConcatAdapter:这个Adapter方便地让我们在一个RecyclerView中连接多个Adapters。
  2. 支持延迟恢复状态:RecyclerView现在支持当内容加载出来后恢复状态。

本文将结合ConcatAdapter的简单使用,由浅入深地讲解ConcatAdapter的高级使用。

简单使用

实现上面是文本列表,下面是按钮列表的效果,如图:

不使用ConcatAdapter实现

在RecyclerView 1.2.0之前,我们可以通过Adapte的getItemViewType方法,设置文本和按钮两种类型。来完成上述效果。伪代码如下,通过TEXT_TYPE和BUTTON_TYPE两种类型,创建不同的视图。

使用ConcatAdapter实现

使用ConcatAdapter实现该效果。只需要创建TextAdapter处理文本列表,创建ButtonAdapter处理按钮列表。通过ConcatAdapter将它们串联起来。代码如下:

优势和劣势

使用ConcatAdapter的优势是Adapter可重用性高,更专注在业务上,不必考虑各种不同ItemType的场景,耦合度低。劣势是,ConcatAdapter不支持不同ItemType交叉出现的场景。

高级进阶

以上就是ConcatAdapter简单使用的全部教程。但是如果你认为ConcatAdapter就这么简单那你就大错特错了。让我们深入源码,玩点更高级的特性吧。

Config类

我们看到ConcatAdapter有如下构造函数。我们注意到Config类是ConcatAdapter的静态内部类。

代码语言:javascript
复制
public ConcatAdapter(Adapter<? extends ViewHolder>... adapters) {
    this(Config.DEFAULT, adapters);
}

public ConcatAdapter(Config config, Adapter<? extends ViewHolder>... adapters) {
    this(config, Arrays.asList(adapters));
}

Config构造函数如下:

代码语言:javascript
复制
Config(boolean isolateViewTypes, StableIdMode stableIdMode) {
    this.isolateViewTypes = isolateViewTypes;
    this.stableIdMode = stableIdMode;
}
代码语言:javascript
复制
public static final Config DEFAULT = new Config(true, NO_STABLE_IDS);

我们注意到默认的Config,isolateViewTypes值为true。

isolateViewTypes含义

要讲清楚isolateViewTypes的含义,那么必须先明白viewType与缓存的关系。我们都知道RecyclerViewPool中是根据viewType缓存ViewHolder的。如果viewType相同,那么它对应的缓存池相同。

RecyclerViewPool缓存示意图如下。每个不同的viewType都有一个属于它自己的缓存。

isolateViewTypes为true。表示ConcatAdapter中的子Adapter的viewType,会被ConcatAdapter隔离开。即使两个子Adapter的中元素的viewType相同,ConcatAdapter会将它们分隔成不同的viewType。从缓存的角度看,即使两个相同的Adapter,它们也无法共用一个缓存池。

isolateViewTypes为false。表示如果viewType相同,那么它们将共用一个缓存池。

不共用缓存

假设有ConcatAdapter,连接了RedAdapter、OrangeAdapter、BlueAdapter、RedAdapter。使用默认Config。isolateViewTypes为true,我们看到RedAdapter的ViewType默认返回1。但是从ConcatAdapter的角度。两个RedAdapter的viewType分别为0和3。

代码语言:javascript
复制
RedAdapter redAdapter1 = xxx;
OrangeAdapter orangerAdapter = xxx;
BlueAdapter blueAdapter = xxx;
RedAdapter redAdapter2 = xxx;

ConcatAdapter concatenated = new ConcatAdapter(redAdapter1, orangerAdapter,blueAdapter,redAdapter2);
recyclerView.setAdapter(concatenated);
共用缓存
代码语言:javascript
复制
RedAdapter redAdapter1 = xxx;
OrangeAdapter orangerAdapter = xxx;
BlueAdapter blueAdapter = xxx;
RedAdapter redAdapter2 = xxx;

//isolateViewTypes为false
ConcatAdapter.Config config = ConcatAdapter.Config.Builder().setIsolateViewTypes(false).build()

ConcatAdapter concatenated = new ConcatAdapter(config, redAdapter1, orangerAdapter,blueAdapter,redAdapter2);
recyclerView.setAdapter(concatenated);

ConcatAdapter的itemType 和子Adapter的itemType一致。

爬坑

坑一

子Adapter的getItemViewType返回默认值。ConcatAdapter isolateViewType设置为false。

TextAdapter和ButtonAdapter和上文一样。

代码语言:javascript
复制
class ConcatAdapterDemoActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_concat_adapter_demo)
        val recyclerView = findViewById<RecyclerView>(R.id.recyclerview)
        recyclerView.layoutManager = LinearLayoutManager(this)
        val config = ConcatAdapter.Config.Builder().setIsolateViewTypes(false).build()
        recyclerView.adapter = ConcatAdapter(
            config,
            TextAdapter(),
            ButtonAdapter()
        )
    }
}

坑二

ConcatAdapter连接多个TextAdapter。isolateViewType设置为true。发现滚动到第二个TextAdapter位置时,又创建了新的TextView。这种情况,相同viewType需要共用缓存。将isolateViewType设置为false。

代码语言:javascript
复制
recyclerView.adapter = ConcatAdapter(
      config,
      TextAdapter(),
      ButtonAdapter(),
      TextAdapter()
)

原理

  1. ConcatAdapter.getItemViewType()
代码语言:javascript
复制
//ConcatAdapter.java
@Override
public int getItemViewType(int position) {
    return mController.getItemViewType(position);
}
  1. ConcatAdapterController.getItemViewType(int globalPosition)
代码语言:javascript
复制
//ConcatAdapterController.java
public int getItemViewType(int globalPosition) {
      //根据globalPosition找到对应的子Adapter的Wrapper
      WrapperAndLocalPosition wrapperAndPos = findWrapperAndLocalPosition(globalPosition);
      int itemViewType = wrapperAndPos.mWrapper.getItemViewType(wrapperAndPos.mLocalPosition);
      releaseWrapperAndLocalPosition(wrapperAndPos);
      return itemViewType;
}
  1. NestedAdapterWrapper.getItemViewType(int localPosition)
代码语言:javascript
复制
int getItemViewType(int localPosition) {
    return mViewTypeLookup.localToGlobal(adapter.getItemViewType(localPosition));
}
  1. IsolatedViewTypeStorage$WrapperViewTypeLookup.localToGlobal()
代码语言:javascript
复制
@Override
public int localToGlobal(int localType) {
    int index = mLocalToGlobalMapping.indexOfKey(localType);
    if (index > -1) {
        return mLocalToGlobalMapping.valueAt(index);
    }
    // get a new key.
    int globalType = obtainViewType(mWrapper);
    mLocalToGlobalMapping.put(localType, globalType);
    mGlobalToLocalMapping.put(globalType, localType);
    return globalType;
}
  1. IsolatedViewTypeStorage$WrapperViewTypeLookup。从代码可以看出在不共享缓存池的情况下。子Adapter的viewType会从0递增对应
代码语言:javascript
复制
int mNextViewType = 0;
int obtainViewType(NestedAdapterWrapper wrapper) {
    int nextId = mNextViewType++;
    mGlobalTypeToWrapper.put(nextId, wrapper);
    return nextId;
}
  1. isolateViewTypes为true的情况下。会使用SharedIdRangeViewTypeStorage$WrapperViewTypeLookup。我们看到 localType和globalType相等。
代码语言:javascript
复制
@Override
public int localToGlobal(int localType) {
    // register it first
    List<NestedAdapterWrapper> wrappers = mGlobalTypeToWrapper.get(
            localType);
    if (wrappers == null) {
        wrappers = new ArrayList<>();
        mGlobalTypeToWrapper.put(localType, wrappers);
    }
    if (!wrappers.contains(mWrapper)) {
        wrappers.add(mWrapper);
    }
    return localType;
}

@Override
public int globalToLocal(int globalType) {
    return globalType;
}
本文参与?腾讯云自媒体分享计划,分享自微信公众号。
原始发表:2021-05-19,如有侵权请联系?cloudcommunity@tencent.com 删除

本文分享自 Android补给站 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与?腾讯云自媒体分享计划? ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 前言
  • 简单使用
    • 不使用ConcatAdapter实现
      • 使用ConcatAdapter实现
        • 优势和劣势
        • 高级进阶
          • Config类
            • isolateViewTypes含义
              • 不共用缓存
              • 共用缓存
          • 爬坑
            • 坑一
              • 坑二
              • 原理
              领券
              问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
              http://www.vxiaotou.com