Skip to content

ComposedAdapter

What's ComposedAdapter?

The ComposedAdapter is an adapter which aggregates multiple adapters into one.

Basic usage of ComposedAdapter

dataSet = new DataSet();

composedAdapter.addAdapter(new AdapterA(new DataSet()));
composedAdapter.addAdapter(new AdapterB(new DataSet()));
composedAdapter.addAdapter(new AdapterC(new DataSet()));

The ComposedAdapter can hold the same child adapter instance multiple times like this;

ComposedAdapter can hold the same child adapter instance multiple times

dataSet = new DataSet();

adapterA = new AdapterA(dataSet);
composedAdapter.addAdapter(adapterA);
composedAdapter.addAdapter(adapterA);

adapterA2 = new AdapterA(dataSet);
composedAdapter.addAdapter(adapterA2);

Item position handling

The ComposedAdapter calls each child adapters as segment, also child adapter's local item position are called as offset.

Segments and Offsets

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.

ItemIdComposer

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.

Bits Usage
bit 63 Reserved
bit 62-56 View type segment
bit 55-28 Group ID
bit 27-0 Child ID

ItemViewTypeComposer

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.

Bits Usage
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.