RecyclerView 加动画的坑

2019-05-21 11:46:05


参考地址 RecyclerView 加动画的坑


有个需求是在recyclerView上的第一个item加呼吸动画,作为一个动画小白,写个呼吸动画就已经很不容易了,呼吸和心跳很像吧,上来我就咔写了个心跳,哈哈哈,才改成了呼吸。
然后加到recyclerView上,我是在adapter上加的。Adapter的holder复用相信大家也都很熟悉了,这个在绘制效率的提高上很重要,也很容易发现一个问题,就是内容混乱的复用。所以常见的处理就是对view加上tag来多次判断,对于visibility之类的设置一定是if...else的写法,光有if是不可以的。
比方说

if (course.isReview()) {
    setVideoBtn(holder, true, stateRes[1], false);
} else {
    setVideoBtn(holder, true, stateResOver, false);
}

然后对于动画,你会发现if else还是不行,因为

holder.getRootView().clearAnimation();

似乎根本不会把我的动画给去掉。
比方说:先定义一个

mBreathAnimator = makeBreathAnimator();
    if (pos == 0) {
        holder.getContainerView().setBackgroundResource(R.drawable.bg_ips_detail_item_breath_shadow);
        holder.getContentBg().setBackgroundColor(mContext.getResources().getColor(android.R.color.transparent));//        final View holderCopy = holder.getRootView();        if (ips.isShouldBreath()) {//            mBreathAnimator.setTarget(holderCopy);
            mBreathAnimator.setTarget(holder.getRootView());
            mBreathAnimator.start();
            isBreathAnimating = true;
        }
    } else {
        Drawable up = ContextCompat.getDrawable(mContext, R.drawable.bg_ips_detail_item);
        Drawable drawableUp= DrawableCompat.wrap(up);
        DrawableCompat.setTint(drawableUp, colors[pos%colors.length]);
        holder.getContentBg().setImageDrawable(drawableUp);
        holder.getRootView().clearAnimation();//        if (mBreathAnimator!=null) {//            mBreathAnimator.setTarget(holder.getRootView());//            LogUtils.d("IPS","pos:"+pos+"-----set end");//            mBreathAnimator.cancel();//            mBreathAnimator.end();//        }
    }

其中那些注释掉的都是我尝试的解决方法,各种不好使。为什么呢,因为这个问题的根本是holder的复用,只要holder pos等于0的动画没有被停止掉,那么它就不会停止。最直观的方法是打log看一眼。

@Overridepublic void onBindViewHolder(ViewHolder holder, int pos) {
    LogUtils.d("onBindViewHolder: 验证是否重用了");
    LogUtils.d( "onBindViewHolder: 重用了"+holder.getRootView().getTag());
    LogUtils.d("onBindViewHolder: 放到了"+mDataSet.get(pos));
    IPSDetailBean.QuestionsBean ips = mDataSet.get(pos);
    ...

没有重用的pos0,tag是null。

08-08 15:27:19.269 D/DeatilAdapter: onBindViewHolder: 验证是否重用了
08-08 15:27:19.270 D/DeatilAdapter: onBindViewHolder: 重用了null
08-08 15:27:19.270 D/DeatilAdapter: onBindViewHolder: 放到了QuestionsBean{id=74, gain_star=0}

重用的pos0,tag是0。

08-08 15:23:06.635 D/DeatilAdapter:  onBindViewHolder: 验证是否重用了
08-08 15:23:06.635 D/DeatilAdapter:  onBindViewHolder: 重用了0
08-08 15:23:06.639 D/DeatilAdapter:  onBindViewHolder: 放到了QuestionsBean{id=57, gain_star=0}

这就是原因,所以我能想到的就是从原因复用上解决了。解决方案如下👇:

    @Override
    public void onViewRecycled(ViewHolder holder) {        super.onViewRecycled(holder);        int pos = (int) holder.getRootView().getTag();
        LogUtils.d("onViewRecycled:"+holder.getRootView().getTag());        if(isBreathAnimating && pos == 0) {
            mBreathAnimator.end();
            LogUtils.d("clearAnimation:");
            holder.getRootView().clearAnimation();
        }
    }

至此,这个recyclerview动画算是告一段落了。
周五提测,找不到解决方案那叫一个烦躁,以为解决不了了。休息了一个周末,倒是让我找到解决方案了,哈哈。

              


  • 2021-04-15 10:32:18

    怎么给 headless chrome添加cookies

    In puppeter you have access to the session cookies through page.cookies(). So once you log in, you could get every cookie and save it in a json file:

  • 2021-04-15 10:51:21

    如何通过Devtools协议拦截和修改Chrome响应数据

    在日常研究中,我们经常碰到大量JavaScript代码,我们首先要深入分析才能了解这些代码的功能及具体逻辑。这些代码代码可能会被恶意注入到页面中,可能是客户送过来需要我们帮忙分析的脚本,也可能是我们的安全团队在网页上找到的引用了我们服务的某些资源。这些脚本通常代码量不大、经过混淆处理,并且我们总是需要经过多层修改才能继续深入分析。

  • 2021-04-19 10:54:39

    block和delegate的区别

    代理 可读性高 大部分可以属性 block 写的代码少 一般作为参数 通知 占用资源

  • 2021-04-19 11:00:23

    浅谈block和delegate的使用

    委托是协议的一种,顾名思义,就是委托他人帮自己去做事。委托是给一个对象提供机会对另一个对象中的变化做出反应或者影响另一个对象的行为。其基本思想是:两个对象协同解决问题,并且打算在广泛的情形中重用。委托指向另一个对象(即它的委托)的引用,并在关键时刻给委托发消息。消息可能只是通知委托发生了某件事情,给委托提供机会执行额外的处理,或者消息可能要求委托提供一些关键的信息以控制所发生的事情。委托的作用主要有两个,一个是传值,一个是传事件。

  • 2021-04-19 11:36:44

    iOS 组件实现方案

    什么才是好架构,为什么要组件,组件设计的优点

  • 2021-04-25 09:53:18

    android debug速度特别慢有时候卡住

    一直提示定在 Starting LLDB server。可能的原因是 Android Studio编译速度太慢了,就会一直卡在Starting LLDB server。可以通过设置 Run/Debug Configurations ——> Debugger ——> Debug type 为 Java 跳过 C/C++的调试,起码实现对 Java 程序的调试

  • 2021-04-25 09:54:19

    sequelize 时区配置

    sequelize 默认情况下, 保存日期时会转换成 +00:00时区,