Data Binding in RecyclerView: Setup and Common Pitfalls
By Rahul Mehta · Published · 4 min read

Data binding turns a RecyclerView row layout into a type-safe class, so you write `binding.title.text = item.title` instead of a chain of `findViewById` calls. On a list of 200 rows that difference is measurable: fewer null checks, fewer casts, and a compiler that catches renamed view IDs before you ship.
The setup is small — one Gradle flag, one layout wrapper tag, one generated class — but the failure modes are consistent. This walkthrough covers enabling data binding, wiring it into an adapter, and the layout mistakes that cause blank rows, stale text, and bind loops in real projects.
Enable data binding without slowing the build
Add `buildFeatures { dataBinding = true }` in the module-level `build.gradle.kts` and sync. The plugin then generates a `*Binding` class for every layout wrapped in `<layout>`. Generation happens at compile time, so expect the first clean build to grow by roughly 5–15 seconds on a mid-sized module — incremental builds are usually unaffected.
If you only need view references and no expressions, consider view binding instead: it is lighter and produces the same `binding.title` accessors. Reach for data binding when you actually want expressions, binding adapters, or two-way binding. Mixing both in one module is legal but confusing; pick one per module and stay consistent.
Keep the Gradle plugin and Android Gradle Plugin versions aligned. Mismatches are a classic source of `Cannot find the setter for attribute` errors that only appear after a dependency bump, especially on older AGP 7.x branches.
- `buildFeatures { dataBinding = true }` — one flag per module
- View binding is enough if you have no expressions
- Clean builds grow; incremental builds rarely do
- Align Gradle and AGP versions before debugging errors
Wrap the row layout correctly
The root of a data-binding layout must be `<layout>`, with the real view hierarchy as its single child. That child can be any ViewGroup, but it must be the only one — two roots produce `The <layout> tag must have only one child` at build time. Namespaces stay on the `<layout>` element, not the inner root.
The generated class name comes from the file name: `item_recipe.xml` becomes `ItemRecipeBinding`. Rename a file and every reference breaks, so decide naming before the layout spreads across the codebase. Prefix row files with `item_` so `ItemRecipeBinding.inflate(inflater, parent, false)` reads naturally in the adapter.
Do not put `tools:` preview attributes on the `<layout>` tag itself; place them on the inner root where the preview renderer expects them. This keeps the layout editor working while the generated code stays clean.
Wire binding into the adapter
Inflate once per view holder, not per bind. Create the holder with `ItemRecipeBinding.inflate(inflater, parent, false)` and pass `binding.root` to the superclass constructor. Store the binding as a field; `onBindViewHolder` then only touches views, which keeps the 16 ms frame budget intact on mid-range devices.
If you use `ListAdapter` with `DiffUtil`, keep the item reference in the holder and bind from `getItem(position)`. Each `onBindViewHolder` call must run against the binding's current state — a common bug is capturing the first item in a lambda inside `init` and never rebinding, so every row shows the same title.
Click listeners belong in the holder, not in `onBindViewHolder`. Set them once in `init` and read the current position via `bindingAdapterPosition`, checking for `NO_POSITION` to survive animations and removals.
- Inflate in `onCreateViewHolder`, bind in `onBindViewHolder`
- Set click listeners once, read `bindingAdapterPosition`
- Never capture a single item in a holder-level lambda
- Guard against `NO_POSITION` during item animations
The layout errors that break lists
Missing `layout_width` or `layout_height` on the inner root is the most common failure: the row renders at zero height and the list looks empty. Data binding does not infer sizes, so set `match_parent` width and `wrap_content` height on the row root explicitly.
Binding expressions that call methods with side effects — formatting, logging, image loading — run on every rebind, and RecyclerView rebinds aggressively during scrolls. Keep expressions to simple field reads and move anything heavier into the adapter or a binding adapter with a clear contract.
Two-way binding on `EditText` inside a list needs extra care: recycled views can commit values to the wrong item when focus moves. Either avoid two-way binding in rows or flush the value on `focusChange` before the holder is recycled.
Binding adapters and image loading
A `@BindingAdapter("imageUrl")` that loads with your image library keeps XML readable, but it must handle reuse: cancel the previous request or check the view's tag before setting a bitmap. Without that, fast scrolls show the wrong thumbnail for a frame or two — visible and reported by users.
Keep binding adapters few and named for intent. One adapter per attribute, with the view type as the first parameter, avoids ambiguity when several adapters match the same attribute. Test them with a small list of mixed image sizes to catch placeholder flicker.
If your list rows grow past roughly 10–15 views each, the binding class itself becomes a memory factor: every holder holds one. That is usually fine, but it is worth knowing when sizing pools — the same math as any heavy row layout.
- Cancel or tag-check before setting an image
- One adapter per attribute, view type first
- Test with mixed image sizes and fast flings
- Each holder holds one binding instance
Debugging generated code
When a binding field is unexpectedly null, open the generated file under `build/generated/.../databinding` and check which IDs were found. A missing `android:id` is silently skipped, so the field never exists and the compiler error points at your code rather than the layout.
Enable `dataBinding { addKtx = true }` if you want Kotlin-friendly extensions, and keep the generated sources out of version control. If builds fail after a layout rename, run a clean build once before chasing the error — stale generated classes mimic real bugs convincingly.
For scroll performance, profile with the layout inspector and check that rebinds stay under a few milliseconds per row. If rows are heavy, the same trade-offs as any list apply, and the sizing advice for adapter pools carries over directly.
| Approach | Setup effort | Type safety | Expressions in XML | Typical row rebind cost |
|---|---|---|---|---|
| findViewById | None | None (casts) | No | Higher: repeated lookups if cached poorly |
| View binding | One Gradle flag | Full | No | Low, same as cached fields |
| Data binding | Gradle flag + <layout> | Full | Yes | Low; expressions add per-rebind work |
| Data binding + adapters | Flag + <layout> + adapters | Full | Yes, with custom attributes | Low if adapters are side-effect free |
| Two-way binding in rows | Flag + <layout> + observables | Full | Yes, both directions | Medium; focus and recycle handling needed |
- Enable data binding only when you need expressions or adapters; view binding covers plain view access with less overhead.
- Inflate the binding in onCreateViewHolder and never capture a single item in a holder-level lambda.
- Set explicit width and height on the row root — zero-height rows are the most common blank-list bug.
- Keep binding expressions side-effect free and cancel image requests to avoid wrong thumbnails during flings.
Specialises in setup guides, build tooling and performance checks for mobile projects. Regularly benchmarks lists, scrolling and startup behaviour to give readers concrete numbers.


