RecyclerView:调整项目的动画大小 [英] RecyclerView: Animate item resize

查看:262
本文介绍了RecyclerView:调整项目的动画大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView.回收器视图的每个项目都可以扩展或不扩展.一次只能展开一个项目.

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.

本质上,我正在尝试在lolipop拨号程序中重新创建历史记录列表.

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

我发现在RecyclerView上使用LayoutTransition 但是我无法正确设置这两种状态之间项目视图更改的动画.

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

我尝试将getItemAnimator().setSupportsChangeAnimations(true)notifyItemChanged(getPosition())结合使用,但是有两个问题:

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

  • 重新创建视图,使过渡变得很奇怪,因为它在移动项目的同时在下一个项目上方淡入淡出.出现的新视图没有调整大小,它已经是完整尺寸.
  • 重新创建视图时,旧视图会淡出,而新视图会淡入,这会使视图背景颜色闪烁.

我还尝试在动画的持续时间内在项目视图上设置LayoutTransition,但是这种方法的问题在于RecycleView的布局会立即更新,并且不会跟随动画.

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 ?

推荐答案

好,所以我找到了一个不涉及调用notifyItemChanged的解决方案,因此该视图不会被替换.缺点是您必须手动检查视图的一致性.为此,我创建了一个小库,可以在ViewHolder中找到5个位置,完全满足我的需求.

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.

诀窍是手动设置高度变化的动画,而不是使用LayoutTransition.

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

可以在此处找到演示项目.

这篇关于RecyclerView:调整项目的动画大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆