RecyclerView: Animate item resize

2020-05-20 07:23发布

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 ?

2条回答
神经病院院长
2楼-- · 2020-05-20 07:37

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.

查看更多
家丑人穷心不美
3楼-- · 2020-05-20 07:39

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.

查看更多
登录 后发表回答