当前位置:网站首页>Concatapter tutorial
Concatapter tutorial
2022-06-30 09:01:00 【Byte station】
1. Preface
2021 year 4 month 7 Japan Android The team officially released RecyclerView 1.2.0 edition . be relative to 1.1.0 edition , It has two major changes :
- Added ConcatAdapter: This Adapter Conveniently, let's work in a RecyclerView Connecting multiple Adapters.
- Supports delayed recovery state :RecyclerView Now it supports restoring the state when the content is loaded .
This article will combine ConcatAdapter Simple use , From simple to deep ConcatAdapter Advanced use of .
2. Easy to use
The implementation above is a list of text , Here's the effect of the button list , Pictured :
2.1 Don't use ConcatAdapter Realization
stay RecyclerView 1.2.0 Before , We can go through Adapte Of getItemViewType Method , Set the text and button types . To achieve the above effect . The pseudocode is as follows , adopt TEXT_TYPE and BUTTON_TYPE Two types of , Create different views .
2.2 Use ConcatAdapter Realization
Use ConcatAdapter Achieve this effect . Just create TextAdapter Working with text lists , establish ButtonAdapter Handle the list of buttons . adopt ConcatAdapter Connect them in series . The code is as follows :
2.3 Advantages and disadvantages
Use ConcatAdapter The advantage is Adapter High reusability , More focused on the business , There's no need to think about the differences ItemType Scene , Low coupling . The disadvantage is ,ConcatAdapter No support for different ItemType Scenes that cross over .
3. Advanced
That's all ConcatAdapter Simple use of all the tutorials . But if you think ConcatAdapter If it's that simple, you're totally wrong . Let's go deep into the source code , Play with more advanced features .
3.1 Config class
We see ConcatAdapter It has the following constructors . We noticed that Config Class is ConcatAdapter The static inner class of .
public ConcatAdapter(Adapter<? extends ViewHolder>... adapters) {
this(Config.DEFAULT, adapters);
}
public ConcatAdapter(Config config, Adapter<? extends ViewHolder>... adapters) {
this(config, Arrays.asList(adapters));
}
Config The constructor is as follows :
Config(boolean isolateViewTypes, StableIdMode stableIdMode) {
this.isolateViewTypes = isolateViewTypes;
this.stableIdMode = stableIdMode;
}
public static final Config DEFAULT = new Config(true, NO_STABLE_IDS);
We notice that the default Config,isolateViewTypes The value is true.
3.2 isolateViewTypes meaning
Make it clear isolateViewTypes The meaning of , Then you have to understand viewType The relationship with cache . We all know RecyclerViewPool According to viewType cache ViewHolder Of . If viewType identical , So its corresponding cache pool is the same .
RecyclerViewPool The cache diagram is as follows . Each is different viewType Each has its own cache .
isolateViewTypes by true. Express ConcatAdapter The son of Adapter Of viewType, Will be ConcatAdapter To separate from each other . Even if two people Adapter Of the elements of viewType identical ,ConcatAdapter Will separate them into different viewType. From the perspective of caching , Even if two are the same Adapter, They also can't share a cache pool .
isolateViewTypes by false. Said if viewType identical , Then they will share a cache pool .
3.1 Cache not shared
Suppose there is ConcatAdapter, Connected to RedAdapter、OrangeAdapter、BlueAdapter、RedAdapter. Use the default Config.isolateViewTypes by true, We see RedAdapter Of ViewType Default return 1. But from ConcatAdapter The angle of . Two RedAdapter Of viewType Respectively 0 and 3.
RedAdapter redAdapter1 = xxx;
OrangeAdapter orangerAdapter = xxx;
BlueAdapter blueAdapter = xxx;
RedAdapter redAdapter2 = xxx;
ConcatAdapter concatenated = new ConcatAdapter(redAdapter1, orangerAdapter,blueAdapter,redAdapter2);
recyclerView.setAdapter(concatenated);
3.2 Shared cache
RedAdapter redAdapter1 = xxx;
OrangeAdapter orangerAdapter = xxx;
BlueAdapter blueAdapter = xxx;
RedAdapter redAdapter2 = xxx;
//isolateViewTypes by false
ConcatAdapter.Config config = ConcatAdapter.Config.Builder().setIsolateViewTypes(false).build()
ConcatAdapter concatenated = new ConcatAdapter(config, redAdapter1, orangerAdapter,blueAdapter,redAdapter2);
recyclerView.setAdapter(concatenated);
ConcatAdapter Of itemType Hezi Adapter Of itemType Agreement .
4. Climb a pit
4.1 A pit
Son Adapter Of getItemViewType Return default .ConcatAdapter isolateViewType Set to false.
TextAdapter and ButtonAdapter As above .
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()
)
}
}
4.2 Pit two
ConcatAdapter Connect multiple TextAdapter.isolateViewType Set to true. Found scrolling to the second TextAdapter When in position , A new TextView. This situation , identical viewType Need to share cache . take isolateViewType Set to false.
recyclerView.adapter = ConcatAdapter(
config,
TextAdapter(),
ButtonAdapter(),
TextAdapter()
)
5. principle
- ConcatAdapter.getItemViewType()
//ConcatAdapter.java
@Override
public int getItemViewType(int position) {
return mController.getItemViewType(position);
}
- ConcatAdapterController.getItemViewType(int globalPosition)
//ConcatAdapterController.java
public int getItemViewType(int globalPosition) {
// according to globalPosition Find the corresponding sub Adapter Of Wrapper
WrapperAndLocalPosition wrapperAndPos = findWrapperAndLocalPosition(globalPosition);
int itemViewType = wrapperAndPos.mWrapper.getItemViewType(wrapperAndPos.mLocalPosition);
releaseWrapperAndLocalPosition(wrapperAndPos);
return itemViewType;
}
- NestedAdapterWrapper.getItemViewType(int localPosition)
int getItemViewType(int localPosition) {
return mViewTypeLookup.localToGlobal(adapter.getItemViewType(localPosition));
}
- IsolatedViewTypeStorage$WrapperViewTypeLookup.localToGlobal()
@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;
}
- IsolatedViewTypeStorage$WrapperViewTypeLookup. As you can see from the code, without sharing the cache pool . Son Adapter Of viewType From 0 Incremental correspondence
int mNextViewType = 0;
int obtainViewType(NestedAdapterWrapper wrapper) {
int nextId = mNextViewType++;
mGlobalTypeToWrapper.put(nextId, wrapper);
return nextId;
}
- isolateViewTypes by true Under the circumstances . Will use SharedIdRangeViewTypeStorage$WrapperViewTypeLookup. We see localType and globalType equal .
@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;
}
6. Last
The final writing was a little hasty . Some points were not explained clearly . If you have any questions, please leave a message . Welcome to WeChat official account. “ Byte station ”. Get more dry articles .
边栏推荐
- Understanding of MVVM and MVC
- Enhance the add / delete operation of for loop & iterator delete collection elements
- Rew acoustic test (II): offline test
- Rew acoustic test (VI): signal and measurement
- [protobuf] protobuf generates cc/h file through proto file
- Flink SQL 自定义 Connector
- vite项目require语法兼容问题解决require is not defined
- Esp32 (7): I2S and I2C drivers for function development
- Unity simple shader
- File upload component on success event, add custom parameters
猜你喜欢
Opencv learning notes-day5 (arithmetic operation of image pixels, add() addition function, subtract() subtraction function, divide() division function, multiply() multiplication function
Esp32 things (3): overview of the overall system design
Rew acoustic test (II): offline test
vim 从嫌弃到依赖(21)——跨文件搜索
codeforces每日5题(均1700)-第三天
Redis design and Implementation (V) | sentinel sentry
Redis design and Implementation (IV) | master-slave replication
Anchorgenerator for mmdet line by line interpretation
技术管理进阶——管理者如何进行梯队设计及建设
Axure制作菜单栏效果
随机推荐
List set export excel table
Unity basic lighting model
JVM调优相关命令以及解释
Anchorgenerator for mmdet line by line interpretation
[untitled]
Rew acoustic test (I): microphone calibration
Design specification for smart speakers v1.0
Qt连接神通数据库
Treatment process record of Union Medical College Hospital (Dongdan hospital area)
C # get the current timestamp
Interference source current spectrum test of current probe
el-input 限制只能输数字
Explanation on the use of password profiteering cracking tool Hydra
快应用中实现自定义抽屉组件
icon资源
CUDA implements matrix replication
C # listbox how to get the selected content (search many invalid articles)
2021-05-06
Icon resources
Influencing factors of echo cancellation for smart speakers