Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RecyclerView: Animate item resize

I have a RecyclerView. Each of its items of the recycler view can be either expanded or not. There can be only one item expanded at the same time.

In essence, I'm trying to re-create the history list in lolipop dialier.

I have found that using a LayoutTransition on RecyclerView makes it crash.

But I have not been able to correctly animate the item view change between both states.

I have tried getItemAnimator().setSupportsChangeAnimations(true) in conjunction with notifyItemChanged(getPosition()) but there is two problems:

  • The view is re-created, making the transition quite weird as it fades in above the next item at the same time as the item is moving. The new view that appears is not resizing, it is already at full size.
  • As the view is re-created, the old view is fade out while the new is fade in, which makes the view background color flicker.

I have also tried setting a LayoutTransition on the item view for the duration of the animation but the problem with this approach is that the layout of the RecycleView updates immediately and does not follow the animation.

I have created a small demo project of this issue for both tries. The projects sources are here.

How can I create a smooth transition on item layout change ?

like image 214
Salomon BRYS Avatar asked Dec 12 '14 14:12

Salomon BRYS


1 Answers

OK, So I have found a solution that does not involve calling notifyItemChanged so the view is not replaced. The con is that you have to manualy check the view consistency. For that, I have created a small library that allows exactly what I was looking for, for 5 loc in the ViewHolder.

The trick is to animate height change manualy rather than using a LayoutTransition.

The demo project can be found here.

like image 129
Salomon BRYS Avatar answered Nov 15 '22 08:11

Salomon BRYS