ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的AI之旅 广告
>上篇「[RecyclerView.Adapter优化了吗?](http://blog.csdn.net/cym492224103/article/details/51113321)」主要讲了RecyclerView.Adapter的优化代码以及添加了item的click方法具体实现原理,这篇在原来的基础上新增列表动画,后续还会扩展更多功能,供大家学习,支持我就Star一下「[BaseRecyclerViewAdapterHelper](https://github.com/CymChad/BaseRecyclerViewAdapterHelper)」。 ##效果如何? ![自定义动画.gif](http://upload-images.jianshu.io/upload_images/972352-60dff17fc9b0491f.gif?imageMogr2/auto-orient/strip) ![缩放+渐现.gif](http://upload-images.jianshu.io/upload_images/972352-3613112a80016b61.gif?imageMogr2/auto-orient/strip) ![从下到上+从左到右.gif](http://upload-images.jianshu.io/upload_images/972352-59c9865417032c00.gif?imageMogr2/auto-orient/strip) ##如何使用? ``` // 一行代码搞定(默认为渐显效果) quickAdapter.openLoadAnimation(); ``` 如果你想换成别的效果你也可以 ``` // 默认提供5种方法(渐显、缩放、从下到上,从左到右、从右到左) quickAdapter.openLoadAnimation(BaseQuickAdapter.ALPHAIN); ``` 如果还是不满意则可以自定义效果 ``` quickAdapter.openLoadAnimation(new BaseAnimation() { @Override public Animator[] getAnimators(View view) { return new Animator[]{ ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1), ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1) }; } }); ``` 使用就是如此简单。 ##如何做到的? >首先先思考两个问题 - **添加动画在哪个方法里面添加?** - **如何控制动画加载次数?** ###添加动画在哪个方法里面添加? onBindViewHolder方法,因为每次填充数据的时候都会调用该方法。 ``` private int mDuration = 300; public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { // 此处代码省略,代码已经在上篇详细讲解过了 if (isOpenAnimation) { BaseAnimation animation = null; if (customAnimation != null) { animation = customAnimation; }else{ animation = selectAnimation; } for (Animator anim : animation.getAnimators(holder.itemView)) { anim.setDuration(mDuration).start(); anim.setInterpolator(mInterpolator); } } } ``` 以上代码,首先判断是否开启动画,然后判断是否是自定义动画还是用户选择的自带动画,然后对动画的操作元素进行遍历执行,执行时间为300毫秒,由于上面说了每次填充数据都会调用,所以如何不判断的话,就会导致上下滑动每次都会重复调用动画,动画本身是会耗费性能的。 ###如何控制动画加载次数? ``` private int mDuration = 300; private int mLastPosition = -1; private boolean isFirstOnly = true; public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { // 此处代码省略,代码已经在上篇详细讲解过了 if (isOpenAnimation) { if (!isFirstOnly || position > mLastPosition) { BaseAnimation animation = null; if (customAnimation != null) { animation = customAnimation; }else{ animation = selectAnimation; } for (Animator anim : animation.getAnimators(holder.itemView)) { anim.setDuration(mDuration).start(); anim.setInterpolator(mInterpolator); } mLastPosition = position; } } } public void setFirstOnly(boolean firstOnly) { isFirstOnly = firstOnly; } ``` 只需要添加一个mLastPosition来存储滑动过的位置,然后判断滑动的位置是否被滑动过,这样就可以避免每次都添加动画了。不过为了满足喜欢动画多过于性能的开发者,如果你想要每次滑动都带动画可以设置isFirstOnly属性即可,默认是不开启的。 ##Animation的设计 ###BaseAnimation ``` public abstract class BaseAnimation { public abstract Animator[] getAnimators(View view); } ``` ###AlphaInAnimation ``` public class AlphaInAnimation extends BaseAnimation { private static final float DEFAULT_ALPHA_FROM = 0f; private final float mFrom; public AlphaInAnimation() { this(DEFAULT_ALPHA_FROM); } public AlphaInAnimation(float from) { mFrom = from; } @Override public Animator[] getAnimators(View view) { return new Animator[]{ObjectAnimator.ofFloat(view, "alpha", mFrom, 1f)}; } } ``` >策略模式定义了一系列的算法,并将每一个算法封装起来,而且使它们还可以相互替换。策略模式让算法独立于使用它的客户而独立变化。 在此采用的是「策略模式」,这样更加灵活,如果需要自定义一个自己的动画,只需要继承BaseAnimation实现getAnimators方法即可。 ``` public class CustomAnimation extends BaseAnimation { @Override public Animator[] getAnimators(View view) { return new Animator[]{ ObjectAnimator.ofFloat(view, "scaleY", 1, 1.1f, 1), ObjectAnimator.ofFloat(view, "scaleX", 1, 1.1f, 1) }; } } ``` 设置即可 ``` quickAdapter.openLoadAnimation(new CustomAnimation()); ``` ##小知识 ###openLoadAnimation方法 ``` public void openLoadAnimation(@AnimationType int animationType) ``` 在写这个方法的时候,想是不是用枚举更合适?但是曾看过多篇性能优化的文章不推荐Enum枚举,因为性能消耗,其实Android有自带的枚举 **官方文档说明,安卓开发应避免使用Enum(枚举类),因为相比于静态常量Enum会花费两倍以上的内存。** Android推荐,以下写法 ``` //先定义 常量 public static final int SUNDAY = 0; public static final int MONDAY = 1; public static final int TUESDAY = 2; public static final int WEDNESDAY = 3; public static final int THURSDAY = 4; public static final int FRIDAY = 5; public static final int SATURDAY = 6; //默认参数 @WeekDays int currentDay = SUNDAY; //用@IntDef "包住" 常量; // @Retention 定义策略 // 声明构造器 @IntDef({SUNDAY, MONDAY,TUESDAY,WEDNESDAY,THURSDAY,FRIDAY,SATURDAY}) @Retention(RetentionPolicy.SOURCE) public @interface WeekDays {} //设置参数 public void setCurrentDay(@WeekDays int currentDay) { this.currentDay = currentDay; } ``` 详细文章可查看 https://noobcoderblog.wordpress.com/2015/04/12/java-enum-and-android-intdefstringdef-annotation/ ###下拉框的引用 >aar包是 Android 的类库项目的二进制发行包。 下拉框引用到别人的工程,引用方式是「arr包」 --- >每次收获一点点,后续还会扩展更多功能,供大家学习,支持我就Star一下「[BaseRecyclerViewAdapterHelper](https://github.com/CymChad/BaseRecyclerViewAdapterHelper)」。