@@ -100,33 +100,50 @@ public CustomItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int po
100100 /* attachToRoot= */ false );
101101 return new CustomItemViewHolder (item );
102102 }
103+
103104 @ Override
104- public void onBindViewHolder (
105- @ NonNull CustomItemViewHolder viewHolder , int position ) {
105+ public void onBindViewHolder (@ NonNull CustomItemViewHolder viewHolder , int position ) {
106106 CustomListItemData data = getItemAt (position );
107- viewHolder .bind (data );
107+ viewHolder .bind (data , position , getItemCount () );
108108 }
109+
110+ @ Override
111+ public void onBindViewHolder (
112+ @ NonNull CustomItemViewHolder viewHolder , int position , @ NonNull List <Object > payloads ) {
113+ if (payloads .isEmpty ()) {
114+ super .onBindViewHolder (viewHolder , position , payloads );
115+ } else {
116+ // Partial update: just re-bind.
117+ CustomListItemData data = getItemAt (position );
118+ viewHolder .bind (data , position , getItemCount ());
119+ }
120+ }
121+
109122 @ Override
110123 public int getItemCount () {
111124 return items .size ();
112125 }
126+
113127 @ NonNull
114128 public CustomListItemData getItemAt (int i ) {
115129 return items .get (i );
116130 }
131+
117132 /**
118133 * Set exclusive selected position.
119134 */
120135 public void setSelectedPosition (int selectedPosition ) {
121136 this .selectedPosition = selectedPosition ;
122137 }
138+
123139 /**
124140 * Return exclusive selected position.
125141 */
126142 public int getSelectedPosition () {
127143 return selectedPosition ;
128144 }
129145 }
146+
130147 /** A ViewHolder that shows custom list items */
131148 public class CustomItemViewHolder extends ListItemViewHolder {
132149 private final TextView textView ;
@@ -137,8 +154,9 @@ public CustomItemViewHolder(@NonNull View itemView) {
137154 textView = itemView .findViewById (R .id .cat_list_item_text );
138155 cardView = itemView .findViewById (R .id .cat_list_item_card_view );
139156 }
140- public void bind (@ NonNull CustomListItemData data ) {
141- super .bind ();
157+
158+ public void bind (@ NonNull CustomListItemData data , int position , int totalCount ) {
159+ super .bind (position , totalCount );
142160 textView .setText (data .text );
143161 cardView .setChecked (data .indexInSection == adapter .getSelectedPosition ());
144162 cardView .setOnClickListener (
@@ -147,9 +165,10 @@ public void bind(@NonNull CustomListItemData data) {
147165 adapter .setSelectedPosition (data .indexInSection );
148166 Toast .makeText (v .getContext (), R .string .mtrl_list_item_clicked , Toast .LENGTH_SHORT )
149167 .show ();
150- adapter .notifyItemChanged (data .indexInSection );
168+ // Pass a payload to prevent the RecyclerView default change animation
169+ adapter .notifyItemChanged (data .indexInSection , "selection_changed" );
151170 if (previouslySelectedPosition != NO_SELECTION ) {
152- adapter .notifyItemChanged (previouslySelectedPosition );
171+ adapter .notifyItemChanged (previouslySelectedPosition , "selection_changed" );
153172 }
154173 });
155174 }
0 commit comments