Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to update RecyclerView item without animation?

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());
        }
    }
like image 610
Sam Avatar asked Aug 09 '17 10:08

Sam


People also ask

How to stop RecyclerView animation?

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.

How do I refresh my kotlin adapter?

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.


2 Answers

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)
like image 182
Rakshit Nawani Avatar answered Oct 20 '22 03:10

Rakshit Nawani


Based on the Rakshit's answer, in Kotlin 1.2 the following code works fine:

notifyItemChanged(position, Unit)
like image 9
j_gonfer Avatar answered Oct 20 '22 02:10

j_gonfer