小编Mus*_*shi的帖子

如何在RecyclerView中使用GridLayoutAnimation?

我正在尝试用新的RecyclerView(使用GridLayoutManager)替换我的GridView,但似乎它不能很好地处理gridLayoutAnimation(ClassCastException: LayoutAnimationController$AnimationParameters cannot be cast to GridLayoutAnimationController$AnimationParameters).它适用于常规布局动画,但因为它是一个网格,所以在平板电脑上完成需要很长时间.

我想要完成的是类似于Hierarchical Timing.如果您查看示例视频,它会显示布局动画从左上角到右下角.常规布局动画将逐行执行动画,因此在更大的网格(例如平板电脑)上花费太多时间来完成.我也尝试过探索ItemAnimator,但这只会像在"不要"的例子中那样同时在所有单元格上运行动画.

有没有办法在RecyclerView中完成此网格布局动画?

这是gridview_layout_animation.xml:

<!-- replace gridLayoutAnimation with layoutAnimation and -->
<!-- replace column- and rowDelay with delay for RecyclerView -->

<gridLayoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:columnDelay="15%"
    android:rowDelay="15%"
    android:animation="@anim/grow_in"
    android:animationOrder="normal"
    android:direction="top_to_bottom|left_to_right"
    android:interpolator="@android:interpolator/linear"
/>
Run Code Online (Sandbox Code Playgroud)

这是动画grow_in.xml:

<set android:shareInterpolator="false"
 xmlns:android="http://schemas.android.com/apk/res/android">
    <scale
        android:interpolator="@android:interpolator/decelerate_quint"
        android:fromXScale="0.0"
        android:toXScale="1.0"
        android:fromYScale="0.0"
        android:toYScale="1.0"
        android:pivotX="50%"
        android:pivotY="50%"
        android:fillAfter="true"
        android:duration="400"
        android:startOffset="200"
    />
</set>
Run Code Online (Sandbox Code Playgroud)

编辑:基于Galaxas0的答案,是一个解决方案,只需要您使用扩展的自定义视图RecyclerView.基本上只覆盖attachLayoutAnimationParameters()方法.这<gridLayoutAnimation>与GridView一样.

public class GridRecyclerView extends RecyclerView {

    public GridRecyclerView(Context context) {
        super(context);
    } …
Run Code Online (Sandbox Code Playgroud)

android layout-animation android-recyclerview

9
推荐指数
1
解决办法
6431
查看次数