---
title: "RecyclerView Trick: Selectively bind ViewHolders with Payloads"
source: "/writing/recyclerview-trick-selectively-bind-viewholders-with-payloads/"
---

Article

# RecyclerView Trick: Selectively bind ViewHolders with Payloads

Andrew Haisting

December 20, 2017

> What if you want to update a single item in a `RecyclerView` but don't want to rebind the entire view? There's actually a [variation](<https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#notifyItemChanged\(int, java.lang.Object\)>) of `notifyItemChanged` that allows us to do just that:

### Before:

Consider this example click handler for a `RecyclerView` item:

After the call to `notifyItemChanged`, the adapter gets notified and `onBindViewHolder` gets called. Unfortunately, rebinding the entire view can lead to some visual quirks*:

GOTCHA!! When we notify our adapter that an item has changed, the entire view gets rebound, causing a flash on the view's image.

What if we only want to update the parts of the view that actually changed? When we notify our adapter, we can pass along a "payload" object:

Then we add logic to our adapter to handle these special "payloads":

### After:

Viola!! Only the favorite icon changes:

### This strategy has a few advantages:

1.  We keep all of our view binding logic contained inside of our `Adapter` classes.
2.  We have flexible options about how to bind: only want to bind certain parts of the View? Sure. Want animations to show the change, but only on certain updates? Yup. Want to chain together multiple calls to `notifyItemChanged` to reflect multiple updates? You betcha, `[onBindViewHolder](<https://developer.android.com/reference/android/support/v7/widget/RecyclerView.Adapter.html#onBindViewHolder\(VH, int, java.util.List\<java.lang.Object\>\)>)` will give you an entire `List<Object>` that has all of your payloads merged together.
3.  We can always fallback to a default update; any time the adapter doesn't know how to handle a certain payload, the view gets fully rebound.

Happy recycling!

*The background flash is actually intended behavior and occurs because of `DefaultItemAnimator`'s implementation of the "change" animation. Calling `[setSupportChangeAnimations(false)](https://developer.android.com/reference/android/support/v7/widget/SimpleItemAnimator.html#setSupportsChangeAnimations\(boolean\))` would also "fix" the background flash. This solution is a bit heavy handed, however. Disabling change animations on the `ItemAnimator` will stop _all_ animations when often times we only want to disable _some_ animations.

_Andrew writes code with_ [_Livefront_](http://livefront.com) _and plays_ [_guitar_](https://www.instagram.com/licksbyandy/) _in his free time._

-   Andrew Haisting

    Software Engineer

    Andrew Haisting

### Tags

[Digital Engineering](/insights/all/?topic=digital-engineering) [Mobile Apps](/insights/all/?topic=mobile-apps)

### Share

-   Copied

## View these next.

[See all](/insights/all/?type=writing)

-   [

    Article

    How To Sabotage Your Project Using Inconsistency

    Collin Flynn

    ](/writing/how-to-sabotage-your-project-using-inconsistency/)
-   [

    Article

    Unit Testing race conditions by creating chaos (Swift)

    Sean Berry

    ](/writing/unit-testing-race-conditions-by-creating-chaos-swift/)
-   [

    Article

    UIApplicationDelegate call sequence reference

    Sean Berry

    ](/writing/uiapplicationdelegate-call-sequence-reference/)
