RecyclerView加载动画

这篇文章用来讲解RecyclerView自定义进入动画



分为三个步骤

1.自定义RecyclerView
2.xml中定义布局动画
3.界面打开后执行进入动画

1.自定义RecyclerView

public class CustomRecyclerView extends RecyclerView {
    public CustomRecyclerView(Context context) {
        super(context);
    }

    public CustomRecyclerView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    @Override
    public void setLayoutManager(LayoutManager layout) {
        if (layout instanceof LinearLayoutManager) {
            super.setLayoutManager(layout);
        }else {
            throw new ClassCastException("请使用LinearLayoutManager");
        }
    }
    @Override
    protected void attachLayoutAnimationParameters(View child, ViewGroup.LayoutParams params, int index, int count) {
        if (getAdapter() != null && getLayoutManager() instanceof LinearLayoutManager){
            LayoutAnimationController.AnimationParameters animationParameters = ( LayoutAnimationController.AnimationParameters)params.layoutAnimationParameters;
            if (animationParameters == null){
                AlphaAnimation alphaAnimation = new AlphaAnimation(0,1);
                alphaAnimation.setDuration(1000);
                animationParameters = new LayoutAnimationController.AnimationParameters();
                params.layoutAnimationParameters = animationParameters;
            }
            animationParameters.count = count;
            animationParameters.index = index;
        }else {
            super.attachLayoutAnimationParameters(child, params, index, count);
        }
    }
}





2.xml中定义布局动画

定义布局动画的xml文件
recycler_animation.xml
Android:delay=”15%”表示延迟时间的百分数,一个一个的动画
android:animationOrder=”normal”表示进入的动画顺序,normal:按顺序,reverse:反向顺序 ,random:随机顺序

<?xml version="1.0" encoding="utf-8"?>
<layoutAnimation xmlns:android="http://schemas.android.com/apk/res/android"
    android:animation="@anim/slide_in_bottom"
    android:delay="15%"
    android:animationOrder="normal"
    />


slide_in_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<translate xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/decelerate_interpolator"
    android:fromYDelta="100%p" android:toYDelta="0"
    android:duration="@android:integer/config_mediumAnimTime"/>




3.界面打开后执行进入动画

我们想在Activity完全打开后执行动画,android5.0以后可以监听到这个完成的动作

 
 @Override//activity进入动画完成时,调用设置recyclerview的进入动画
    public void onEnterAnimationComplete() {
        super.onEnterAnimationComplete();
        mAdapter = new MyAdapter();
        mRecycler.setAdapter(mAdapter);
        mRecycler.scheduleLayoutAnimation();//安排动画
    }


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值