With RecyclerView's ConcatAdapter, what do Config's isolateViewTypes and StableIdMode control, and which ViewHolder position should a click listener read?
answer
- several adapters, one list
- types rewritten unless told otherwise
- isolated types, isolated holder pools
- stable ids set only through Config
- binding position versus absolute position
basics
~10 sConcatAdapter shows several adapters in sequence. isolateViewTypes (default true) rewrites view types so adapters cannot share holders; StableIdMode (default NO_STABLE_IDS) decides whether and how ids are exposed. Click listeners use getBindingAdapterPosition.
solid answer
~30 s`ConcatAdapter` (RecyclerView 1.2.0) chains adapters such as a header, a list and a footer, and offsets their notify calls. Its `Config` decides two things. `isolateViewTypes`, true by default, rewrites each adapter's view types so they cannot collide, so each adapter keeps an isolated holder pool; set it false when adapters render the same row type and should share holders. `StableIdMode` defaults to `NO_STABLE_IDS`; `ISOLATED_STABLE_IDS` and `SHARED_STABLE_IDS` require every wrapped adapter to have stable ids, and `setHasStableIds` on the ConcatAdapter throws. In a listener, `getBindingAdapterPosition()` indexes the bound adapter's data, while `getAbsoluteAdapterPosition()` counts the whole list.
code
kotlin · 13 linesval config = ConcatAdapter.Config.Builder()
.setIsolateViewTypes(false) // both book sections return R.layout.item_book
.setStableIdMode(ConcatAdapter.Config.StableIdMode.NO_STABLE_IDS)
.build()
val adapter = ConcatAdapter(config, statsAdapter, downloadedAdapter, cloudAdapter)
recyclerView.adapter = adapter
// In the downloaded-books adapter's ViewHolder
itemView.setOnClickListener {
val pos = bindingAdapterPosition // index within downloadedAdapter
if (pos != RecyclerView.NO_POSITION) onBookClick(pos)
}go deeper
Recall that ConcatAdapter shows several adapters one after another, such as a header, a list and a footer.
Explain isolateViewTypes and holder sharing, the three StableIdMode values, and why getBindingAdapterPosition replaced getAdapterPosition.
Catch the review bugs: absolute positions indexing one adapter, isolated types where holders should be shared, stable ids set on the wrong object.
Decide when a screen is a chain of small adapters and when interleaved rows call for one adapter with view types.
## What ConcatAdapter is for The audiobook library screen has three parts: a header with the user's listening stats, the list of books, and a footer offering "Browse the store". Before RecyclerView 1.2.0 the usual approach was one adapter with extra view types and position arithmetic. **`ConcatAdapter`** joins several adapters into one and presents their items in order, so each part keeps its own simple adapter: ```kotlin val adapter = ConcatAdapter(statsAdapter, booksAdapter, storeFooterAdapter) ``` It shipped in 1.2.0. During that release's alphas it was called `MergeAdapter` and was renamed to avoid confusion with data merging. When a wrapped adapter calls a `notify` method, ConcatAdapter offsets the positions before passing them to RecyclerView. If a wrapped adapter calls `notifyDataSetChanged()`, ConcatAdapter calls it for the whole list, which is one more reason its javadoc recommends `ListAdapter` for the wrapped adapters. ## Config: the two decisions `ConcatAdapter.Config`, built with `ConcatAdapter.Config.Builder`, holds two settings. `Config.DEFAULT` is isolated view types with no stable ids, and since 1.2.0-rc01 the builder starts from the same defaults. ### isolateViewTypes - **`true` (default):** ConcatAdapter rewrites each wrapped adapter's view types so they cannot collide. The cost: **each adapter gets its own isolated pool of ViewHolders, with no reuse between adapters**. - **`false`:** view types pass through unchanged and holders are shared between adapters that report the same type. Use it when two adapters really render the same row, for example "Downloaded" and "In the cloud" sections that both show book rows. The javadoc's suggested pattern is returning the layout id from `getItemViewType`. Every adapter reporting a type must then accept a holder of that type that another adapter created. ### StableIdMode | Mode | `hasStableIds()` | Requirement | Ids reported | |---|---|---|---| | `NO_STABLE_IDS` (default) | false | none; wrapped adapters' ids are ignored with a logged warning | none | | `ISOLATED_STABLE_IDS` | true | every wrapped adapter must have stable ids | rewritten per adapter so two adapters cannot clash; `ViewHolder.getItemId()` may differ from the adapter's `getItemId` | | `SHARED_STABLE_IDS` | true | every wrapped adapter must have stable ids | passed through; adapters must never return the same id unless an item moved between them | Adding an adapter without stable ids in either stable mode throws an `IllegalArgumentException`. Calling `setHasStableIds` on the ConcatAdapter itself throws an `UnsupportedOperationException`: stable ids are configured only through `Config`. ## Positions inside a ConcatAdapter This is where most ConcatAdapter bugs live. A ViewHolder has two positions: 1. **`getBindingAdapterPosition()`** is the position inside the adapter that bound it: the book's index in `booksAdapter`. Use it to index that adapter's data in a click listener. 2. **`getAbsoluteAdapterPosition()`** is the position in the whole RecyclerView, header included. `getAdapterPosition()` was deprecated in 1.2.0 precisely because it was ambiguous here. A click handler in the books adapter that uses the absolute position opens the wrong book, shifted by the header's item count. `getBindingAdapter()` tells you which adapter bound a holder when one listener serves several. For code that has no holder, such as a `GridLayoutManager.SpanSizeLookup` that must make the header and footer full width, RecyclerView 1.3.0 added `ConcatAdapter.getWrappedAdapterAndPosition(globalPosition)`, which returns the wrapped adapter and the local position. ## Dynamic sections - `addAdapter(adapter)` and `addAdapter(index, adapter)` insert a section, and `removeAdapter(adapter)` drops one; ConcatAdapter dispatches an item range insert or removal for that section's items, so the item animator can animate them. - A footer that appears only in some states can be added and removed as an adapter, or kept as an adapter that holds zero or one item. - `getAdapters()` returns the current list of wrapped adapters. ## When not to use it - When rows from different parts must interleave (a header every few books), a single adapter with view types is simpler. - When sections are pages loaded on demand, the Paging library's adapter and its load-state footer are covered in the lesson on paging. The common review findings are default isolated types where sharing was intended (two adapters inflating the same row with no holder reuse) and an absolute position used to index one adapter's data.
- Why does a ConcatAdapter call notifyDataSetChanged for the whole list when one wrapped adapter calls it?Its javadoc says turning a wrapped adapter's `notifyDataSetChanged()` into a range change would confuse RecyclerView, so it invalidates everything. The whole list then rebinds, header and footer included, which is why it recommends `ListAdapter` for wrapped adapters.
- In ConcatAdapter's ISOLATED_STABLE_IDS mode, why can ViewHolder.getItemId() differ from the wrapped adapter's getItemId?Two wrapped adapters may return the same id because they know nothing about each other, so ConcatAdapter rewrites ids per adapter before reporting them to RecyclerView. Code that needs the adapter's own id should ask that adapter, not the holder.
- ConcatAdapter shares ViewHolders between adapters by default
- Calling setHasStableIds(true) on the ConcatAdapter turns on stable ids
- getAbsoluteAdapterPosition indexes the data of the adapter that bound the row
- ConcatAdapter merges items from its adapters into one interleaved, sorted list
- SHARED_STABLE_IDS rewrites ids so adapters never clash