ComposedAdapter is an adapter which aggregates multiple adapters into one.
dataSet = new DataSet(); composedAdapter.addAdapter(new AdapterA(new DataSet())); composedAdapter.addAdapter(new AdapterB(new DataSet())); composedAdapter.addAdapter(new AdapterC(new DataSet()));
ComposedAdapter implementation on GitHub
ComposedAdapter can hold the same child adapter instance multiple times like this;
dataSet = new DataSet(); adapterA = new AdapterA(dataSet); composedAdapter.addAdapter(adapterA); composedAdapter.addAdapter(adapterA); adapterA2 = new AdapterA(dataSet); composedAdapter.addAdapter(adapterA2);
Item position handling¶
ComposedAdapter calls each child adapters as segment, also child adapter's local item position are called as offset.
Item ID and ViewType handling¶
When merging adapters, we must take care about item IDs. They have to be unique in entire the dataset, but the problem is child datasets may contains the duplicated IDs. The
ItemIdComposer is used to manage this problem.
This utility class provides several static methods to handle the packed item ID value.
Item IDs are expressed by 64 bits length integer in RecyclerView, so it can be embed multiple information by using bit operation technique.
ItemIdComposer divides 64 bits into four chunks; view type segment, group ID, child ID and reserved bit.
|bit 62-56||View type segment|
|bit 55-28||Group ID|
|bit 27-0||Child ID|
ItemIdComposer implementation on GitHub
long ItemIdComposer.composeSegment(int segment, long wrappedId)
int ItemIdComposer.extractSegmentPart(long composedId)
long ItemIdComposer.extractExpandableGroupIdPart(long composedId)
long ItemIdComposer.extractExpandableChildIdPart(long composedId)
long ItemIdComposer.extractWrappedIdPart(long composedId)
boolean ItemIdComposer.isExpandableGroup(long composedId)
long ItemIdComposer.composeExpandableGroupId(long groupId)
long ItemIdComposer.composeExpandableChildId(long groupId, long childId)
Item view type has similar problem like item ID. The
ItemViewTypeCompser manages packed item view type value that
ItemIdComposer doing it for item ID.
Item view types are expressed by 32 bits integer in RecyclerView, and
ItemViewTypeCompser divides it into three chunks; expandable group flag, view type segment and wrapped view type code.
|bit 31||Expandable group flag (1: expandable group / 0: normal item)|
|bit 30-24||View type segment|
|bit 27-0||Wrapped view type code|
How to migrate to WRAPPED adapter?¶
Need to change several things to use your adapter wrapped with
ComposedAdapter. Refer to the Tweak your Adapter to support adapter wrapping page for more details.