滚动时动画RecyclerView
有没有什么办法,当我把它的滚动动画一个RecyclerView的要素是什么?
Is there any way to animate the elements of a RecyclerView when I scroll it?
我看了看 DefaultItemAnimator
和 RecyclerView.ItemAnimator
,但动画似乎只是叫若数据集已更改,请纠正我,如果我错了。
I took a look at DefaultItemAnimator
and RecyclerView.ItemAnimator
, but that animations seems to be only called if the dataset has changed, please correct me if I am wrong.
我对 RecyclerView.ItemAnimator.animateMove有点糊涂了()
时,它叫什么名字?我把一些断点成类,但他们没有停止我的应用程序。
I'm a little confused about RecyclerView.ItemAnimator.animateMove()
when is it called? I put some breakpoints into that class but none of them stops my app.
但回到我的问题我怎么能动画RecyclerView?我想,一些元素有另一种不透明,依赖于一些自定义规则。
However back to my question how can I animate the RecyclerView? I want that some elements have another opacity, depended on some custom rules.
我做了一些更多的reaseach似乎动画此举正是我要找的。这种方法是从 dispatchLayout称为()
。下面是该方法的javadoc的:
I did some more reaseach it seems that animation move is exactly that what I'm looking for. That methods are called from dispatchLayout()
. Here is the javadoc of that method:
各地layoutChildren()处理动画所造成的布局更改包装。
动画的假设工作,有五种不同类型的项目
在打法:
持久性:项目是可见的前后布局
删除:项目是布局前可见的是由应用程序中删除
增加:项目布局之前并不存在,并通过应用程序
添加
消失:在设置前/后的数据存在的项目,而是从改变
可见在布局的过程不可见(它们被移出
画面的其他变化的副作用)
出现:在之前设定的数据存在项/后,而是从改变
非可见的布局的过程中可见的(它们被移动了上
画面的其他变化的副作用)
总体方法计算出哪些项目存在前/布局后
推断五个以上国家对每个项目之一。然后,动画
都设置了相应的:
持久性的观点移动({@link ItemAnimator#animateMove(ViewHolder,INT,INT,INT,INT)})
移除的视图将被删除({@link ItemAnimator#animateRemove(ViewHolder)})
增加的视图中添加({@link ItemAnimator#animateAdd(ViewHolder)})
消失的意见移出屏幕
出现意见移动屏幕上的
Wrapper around layoutChildren() that handles animating changes caused by layout. Animations work on the assumption that there are five different kinds of items in play:
PERSISTENT: items are visible before and after layout
REMOVED: items were visible before layout and were removed by the app
ADDED: items did not exist before layout and were added by the app
DISAPPEARING: items exist in the data set before/after, but changed from visible to non-visible in the process of layout (they were moved off screen as a side-effect of other changes)
APPEARING: items exist in the data set before/after, but changed from non-visible to visible in the process of layout (they were moved on screen as a side-effect of other changes)
The overall approach figures out what items exist before/after layout and infers one of the five above states for each of the items. Then the animations are set up accordingly:
PERSISTENT views are moved ({@link ItemAnimator#animateMove(ViewHolder, int, int, int, int)}) REMOVED views are removed ({@link ItemAnimator#animateRemove(ViewHolder)})
ADDED views are added ({@link ItemAnimator#animateAdd(ViewHolder)})
DISAPPEARING views are moved off screen
APPEARING views are moved on screen
到目前为止,我在寻找持久的,消失与出现,但方法是从来没有所谓,因为这条线在这里:
So far I'm looking for PERSISTENT, DISAPPEARING and APPEARING, but that methods are never called because of this line here:
boolean animateChangesSimple = mItemAnimator != null && mItemsAddedOrRemoved
&& !mItemsChanged;
mItemsAddedOrRemoved
只是一直false,所以没有一个回调都曾经达到了。不知道如何正确设置设置的标志?
mItemsAddedOrRemoved
is simply always false so none of that callback are ever reached. Any idea how to set set flag correctly?
我结束了在使用 OnScrollListener
在自定义的动画动画它( )
方法。在我的情况是code仅需2ms的,这样是没有问题的60fps的。
I ended in using an OnScrollListener
and animating it in a custom animate()
method. In my case that code takes just 2ms so that is no problem for the 60fps.
recyclerView.setOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(int newState) {
if(newState == RecyclerView.SCROLL_STATE_IDLE) {
// special handler to avoid displaying half elements
scrollToNext();
}
animate();
}
@Override
public void onScrolled(int dx, int dy) {
animate();
}
});