Data Binding for RecyclerView in Kotlin: A Practical Setup
By Rahul Mehta · Published · 4 min read

Data binding is often sold as magic, but in a RecyclerView it does two concrete things: it inflates a layout and hands you typed references to views, and it lets you push model fields straight into those views with expressions. On a list screen with 10-20 row types, that removes a surprising amount of boilerplate from adapters.
This article walks through the setup, a complete Kotlin adapter, and the cases where data binding is the wrong tool. If you are still wiring up your environment, the Flutter installation checklist covers the equivalent setup steps on the Flutter side; here we stay in Kotlin and Android.
Enable Data Binding and Convert the Row Layout
In your module-level build.gradle, add buildFeatures { dataBinding = true } inside the android block. In older projects you may still see dataBinding { enabled = true }, which works but is deprecated. Sync, then wrap your row layout's root element in a <layout> tag. The root must be the layout element itself, so your previous root view becomes its single child.
Inside <layout>, add a <data> block declaring the variables your expressions use, for example a variable named item of type com.example.app.model.RowItem. Keep the row layout flat; a data-bound layout that nests five LinearLayouts is slower to inflate than a ConstraintLayout with the same views. A typical list row should stay under 15 views.
Once you rebuild, Android generates a binding class named after the file: row_item.xml becomes RowItemBinding. You can now call RowItemBinding.inflate(inflater, parent, false) and get typed fields for every id. No findViewById, no nullable casts, and renaming a view id breaks the build instead of crashing at runtime.
- Use buildFeatures { dataBinding = true } in the module build.gradle
- Wrap the existing root view in <layout>, keeping one child root
- Declare variables in <data> with fully qualified types
- Keep row layouts under roughly 15 views for fast inflation
A Binding-Aware Adapter in Kotlin
The adapter extends RecyclerView.Adapter<RowViewHolder>, where RowViewHolder holds a RowItemBinding. In onCreateViewHolder you call RowItemBinding.inflate(inflater, parent, false) and wrap it. In onBindViewHolder you set binding.item = currentItem, then call binding.executePendingBindings() so the row is correct before the frame is drawn.
executePendingBindings matters most in lists. Without it, expressions evaluate on the next frame, and during fast scrolls you can see one frame of stale text. The cost is small: it simply forces evaluation now. Keep long-running work out of expressions; a formatter call that allocates a DateFormat per row is a common source of jank.
For click handling, pass a lambda into the adapter constructor and set it on the binding inside onBindViewHolder rather than in the XML. Listeners in XML are harder to trace and keep a reference to the model, which is exactly how leaks creep into long-lived adapters.
- Hold the generated binding in the ViewHolder, not the item view
- Call executePendingBindings() after setting the item in onBindViewHolder
- Prefer adapter constructor lambdas over XML onClick attributes
- Clear the binding reference in onViewRecycled if you hold extra state
Expressions: Useful, But Keep Them Thin
Simple expressions like android:text="@{item.title}" or android:visibility="@{item.isNew ? View.VISIBLE : View.GONE}" are the sweet spot. They remove small view-state blocks that otherwise repeat in every adapter. Null-safe access with the ?? operator also works, so @{item.subtitle ?? @string/no_subtitle} is fine and avoids a null check in Kotlin.
Avoid arithmetic chains and string concatenation across many fields in XML. Each expression becomes generated code that is harder to debug than the Kotlin equivalent, and stack traces point into generated binding classes. If a row needs three or more derived values, compute them once in the model or a view-model and bind the results.
Two-way binding (@={}) is tempting for edit fields inside list rows. In practice it complicates recycling because a recycled row briefly writes old values back into the model. Use one-way binding for list rows and handle edits with explicit listeners.
Data Binding vs View Binding vs findViewById
View binding gives you typed view references with zero expressions and faster builds. Data binding adds expressions, variables and (optionally) two-way binding, at the cost of annotation processing on every build. For a list of simple rows, view binding is often enough and keeps compile times lower on large projects.
findViewById still works, but on a row with 8-10 views it means 8-10 lookups per created holder plus casts. The difference is negligible per row, yet it accumulates across a list of several hundred items and adds noise to the adapter. Either binding approach removes that noise and makes the row layout the single source of truth.
If you are choosing between list containers at the same time, the trade-offs in Scrolling Widgets Compared: ListView, GridView and CustomScrollView mirror the binding decision: pick the simplest tool that covers your row complexity.
- View binding: typed ids, no expressions, fastest builds
- Data binding: typed ids plus expressions and variables
- findViewById: fine for one-off screens, noisy in adapters
- Two-way binding: avoid inside recycled list rows
Performance and Build-Time Realities
Binding inflation on a simple row typically adds a small fraction of a millisecond versus a plain inflate; the real cost is in complex expressions and deep view hierarchies. On mid-range devices, keep row layouts shallow and measure with a release build, because debug builds distort inflation numbers heavily.
Build time is the bigger tax. Enabling data binding on a large module can add tens of seconds to clean builds, and incremental builds slow noticeably when many layouts use expressions. If your team builds often, confine data binding to the feature module that needs it rather than the whole app.
Recycling behaviour does not change: the adapter still creates and binds holders the same way, so sizing your adapter and prefetch correctly matters more than the binding choice. The guidance in How to Size a RecyclerView Adapter for Smooth Lists applies unchanged.
Common Mistakes to Avoid
The most common mistake is setting the item variable but never calling executePendingBindings, then blaming the adapter for flickering text. The second is putting business logic in XML expressions, which turns a layout file into a poorly debugged script. Both are easy to fix once you know they are pitfalls.
Another frequent issue is forgetting that binding classes are generated per layout file, so renaming a layout breaks imports across the module. Keep layout names stable and descriptive. Finally, do not inflate a data-bound layout with the plain LayoutInflater overload that lacks attachToParent; it produces wrong layout params and rows that ignore match_parent.
| Approach | Typed view access | Expressions in XML | Build-time cost | Best fit |
|---|---|---|---|---|
| findViewById | No, manual casts | No | None | Tiny screens, prototypes |
| View binding | Yes | No | Low | Simple to medium rows |
| Data binding (one-way) | Yes | Yes | Medium | Rows with many conditional fields |
| Data binding (two-way) | Yes | Yes | Medium to high | Forms outside recycled lists |
| Data binding + view binding | Yes | Only where needed | Medium | Large modular apps |
- Enable data binding in build.gradle, wrap the row in <layout>, and declare typed variables in <data>.
- Set binding.item in onBindViewHolder and call executePendingBindings to avoid stale frames.
- Keep XML expressions to simple field bindings; compute derived values in Kotlin.
- Choose view binding for simple rows and reserve data binding for rows that genuinely need expressions.
Specialises in setup guides, build tooling and performance checks for mobile projects. Regularly benchmarks lists, scrolling and startup behaviour to give readers concrete numbers.


