9

我已经实现了一个RecyclerView可以添加和删除项目的地方。我希望将添加的项目添加到倒数第二个位置,并且每当我添加一个新项目时,动画运行良好。也就是说,最后一个项目向下移动,为新项目留出空间淡入。

当我删除一个项目时,出现了一个我不知道如何解决的问题。我希望它的行为方式是:

  1. 淡出被删除的元素,
  2. 向上移动它下面的所有项目。

实际发生的是,首先,最后一个项目消失,然后动画的其余部分发生。当被删除元素下方的项目向上移动时,最后一个项目重新出现为来自墙后。

对我来说,似乎RecyclerView缩小到“动画后”高度,然后执行动画。

我还没有定义ItemAnimator,所以DefaultItemAnimator必须是使用的那个。我看过这个视频supportsPredictiveItemAnimations,并在 的自定义实现中覆盖了该方法LinearLayoutManager,但它没有修复它。

4

2 回答 2

5

I already reported the problem through Google Issue Tracker here

I hope we can get a fix soon! As you say It seems very related to a possible race condition between the measure updates for the recyclerview and the animation when your recyclerview is wrapping it's content to calculate it's height.

This article explains the problem in a really detailed way also.

于 2017-05-24T09:25:37.740 回答
0

也许有点晚了,但我能够通过将 RecyclerView 项目动画器设置为 null 来解决这个问题,然后在setList(list)我的适配器的函数中调度一个转换,如下所示:

Transition transition = new AutoTransition();
transition.setDuration(200); // Or any duration you want
TransitionManager.beginDelayedTransition(mRootViewGroup, transition);

其中 mRootViewGroup 是包含 RecyclerView 的视图组。

这个问题也可以通过将你的 layout_height(或宽度,取决于滚动方向)设置为 wrap_content 以外的东西来解决,但是如果像我一样,你需要将你的 recyclerview 高度设置为 wrap_content,这可能是你的解决方案。

不确定它是否也能解决您的问题,但我想我不妨分享一下对我有用的方法。

于 2021-01-09T09:32:14.957 回答