RecyclerView Animations and Behind the Scenes (Android Dev Summit 2015)

Android Developers · 110K views · 2015 · 45:35

Open on YouTube

Software architecture & designWeb developmenttalk

Why Stack Overflow recommends it

Stack Overflow users link it when answering RecyclerView questions about ItemAnimator, notifyItemXYZ calls and why getAdapterPosition() and adapter position handling behave the way they do.

Recommended by 6 people across 6 different months, 2016 to 2021.

What it is

A 45-minute Android Dev Summit 2015 talk in which Chet Haase and Yigit Boyar, both on Google's Android UI Toolkit team, explain how RecyclerView animations work internally, including predictive animations, ItemAnimator and how to extend them. It covers the design considerations behind the animation system and how adapter, layout manager and animator interact.

What people said

Recommends Raghunandan · 2016-03 · Stack Overflow · on “RecyclerView getAdapterPosition() returns -1 on a callback so I can't show the new appearance for the item”

Yes the recyclerview animations sample by yigit boyar has **int pos = getAdapterPosition(); if (pos != RecyclerView.NO_POSITION) {// do something and then notifyItemChanged(pos)}`**. although not related to the topic watching this [this video] might help

Recommends Sree · 2016-11 · Stack Overflow · on “How to delete object from onclick in recyclerview?”

You need to implement the OnClickListener inside the onCreateView() method of your RecyclerView.Adapter . View v = LayoutInflater.from(parent.getContext()) .inflate(R.layout.holder_layout, parent, false); final CustomViewHolder holder = new CustomViewHolder(v); holder.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int adapterPos = holder.getAdapterPosition(); if (adapterPos != RecyclerView.NO_POSITION) { //now you can use adapterPos to get the item in your list } } }); This video [this video] will elaborate.

Recommends Eugen Pechanec · 2017-03 · Stack Overflow · on “RecyclerView - Horizontal LinearLayoutManager create / bind methods called way too often”

Can recommend [this video] on animations inside RecyclerView. Basically you should do all animation based on a `payload` in a custom `ItemAnimator`. I heve never implemented this so I can't provide a full fledged answer.

Recommends Vicky · 2017-12 · Stack Overflow · on “How to remove all items in RecyclerView Adapter with Animation”

The proper way of doing animations in `RecyclerView` is through `ItemAnimator`. Don't put animations in adapter because it's not adapter's responsibility to animator layout changes. check this video : link [this video] . For more details , check this repository : repo [LINK]

Recommends Ben P. · 2019-05 · Stack Overflow · on “RecyclerView with CardView Items, notifyItemChanged and shadow problem”

I believe you are going to have to implement your own item animator. You could pass a `notifyItemChanged()` payload to disable the entire default animation (see [LINK] ), but then you wouldn't get a crossfade on the card contents. This Google presentation [this video] covers how to do custom animations (including color fading).

Recommends ashakirov · 2021-09 · Stack Overflow · on “MotionLayout animation item doesn't work inside RecyclerView”

You should use RecyclerView.ItemAnimator [LINK] for recyclerView animations. See recycerView.setItemAnimator [LINK] method. It's wrong to animate ViewHolder views directly into adapter because recyclerView reuse views. While you scroll recyclerView bind old views to new items, but recyclerview doesn't know that old views are in process of animation, that's why it becomes messy. Here is good video [this video] which explains how recyclerView and ItemAnimator works. Here is nice tutorial [LINK] with custom ItemAnimator implementation.

Cites Much Overflow · 2016-02 · Stack Overflow · on “Recycler View Fatal Exception: java.lang.ArrayIndexOutOfBoundsException”

Looks like you were trying to call getAdapterPosition() while Recycler View layouts are not calculated (possibly triggered by a notifyDataSetChanged() call). always handle it like this final int position = getAdapterPosition(); if(positon != RecyclerView.NO_POSITION){ // you can trust the adapter position // do whatever you intend to do with this position } The documentation clearly states the following if you want to access the item in the adapter on a ViewHolder click, you should use getAdapterPosition(). Beware that these methods may not be able to calculate adapter positions if notifyDataSetChanged() has been called and new layout has not yet been calculated. For this reasons, you should

Cites eimmer · 2016-02 · Stack Overflow · on “RecyclerView Q&A”

I'm creating a Q&A where each question is a card. The answer starts showing the first line, but when its clicked it should expanded to show the full answer. When an answer is expanded/collapsed the rest of the RecyclerView should animate to make room for the expansion or collapse to avoid showing a blank space. I watched the talk on RecyclerView animations [this video] , and believe I want a custom ItemAnimator, where I override animateChange. At that point I should create an ObjectAnimator to animate the height of the View's LayoutParams. Unfortunately I'm having a hard time tying it all together. I also return true when overriding canReuseUpdatedViewHolder, so we reuse the same viewholder. @Ove

Cites Daniel Reina · 2016-03 · Stack Overflow · on “RecyclerView remove animation bug”

I have implemented a RecyclerView where I can add and delete items. I want the added item to be added on the second last position and, whenever I add a new item, the animation runs well. That is, the last item moves downwards, letting space for the new item to fade in. When I remove an item there is a problem that I don't know how to fix. How I want it to behave is: fade out the deleted element, move upwards all the items below it. What actually happens is that, first thing, the last item disappears, and then the rest of the animation takes place. When the items below the deleted element move upwards, the last item reappears as coming from behind a wall. To me it seems as if the RecyclerView

Cites Essam Ewaisha · 2016-04 · Stack Overflow · on “Android recyclerview view holder's internal view's animations”

So the problem I was having is that I was making the animation in Adapter. Which was stated here [this video] as a wrong approach. What we want to do in this case is to create a class that extends DefaultItemAnimator, override the animateChange method and place your custom animation there. Now the problem with that is that you don't know the state of your view holder. Sometimes you want to animate the holder when a button is clicked. To solve that you can use flags in your view holder, that you will have a reference to in the animateChange method. Then you'd want to call recyclerView.setItemAnimator(animater) and pass in an instance of your class.