I have a RecyclerView
. When I click a button inside an item in RecyclerView
, I want to change the color of a View
in that item. The following is my code and it works fine. But, the problem is the item will have an animation which is ugly. I want to update the item without the animation. How should I do that? By the way, I don't want to turn off the animation, only for this click event.
public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
public ImageView imageView;
public Button button;
public ItemViewHolder(View view) {
super(view);
//do something
button.setOnClickListener(this);
}
@Override
public void onClick(View view) {
//change color
notifyItemChanged(getAdapterPosition());
}
}
How to remove scroll effect from RecyclerView android? You can open or close it. Basically, you need to add android:overScrollMode=“never” line to your RecyclerView.
To refresh the ListView in Android, call notifyDataSetChanged() method on the Adapter that has been set with the ListView. Also note that the method notifyDataSetChanged() has to be called on UI thread.
Try this
notifyItemChanged(position, Object);
This will update the position without animating it as we are passing our Object in it.
Try this and do let me know.
For Kotlin
you can use
notifyItemChanged(int position, @Nullable Object payload)
Based on the Rakshit's answer, in Kotlin 1.2 the following code works fine:
notifyItemChanged(position, Unit)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With