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 ?
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.
You should just use notifyItemChanged(getPosition(), new Object())
instead.
In your adapter, override onBindViewHolder(GigExtraViewHolder holder, int position, List<Object> payloads)
, if payloads is null or empty do your original bind logics, else, just do your own expand/collapse animation.
来源:https://stackoverflow.com/questions/27446051/recyclerview-animate-item-resize