Skip to content

处理CoordinatorLayout动画抖动问题

纹身机主页部分的问题:

https://zhuanlan.zhihu.com/p/170614240

Code Part:

  • 博客里面真正有效解决问题的,是以下这个Java文件。
java
package com.printmaster.aidraw.behavior;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.OverScroller;

import androidx.annotation.NonNull;
import androidx.coordinatorlayout.widget.CoordinatorLayout;

import com.google.android.material.appbar.AppBarLayout;

import java.lang.reflect.Field;

/**
 * <h3>给CoordinatorLayout使用的一个{@link AppBarLayout.Behavior},用于修复CoordinatorLayout的抖动情况,参考下面这篇文章实现的</h3>
 * <p><a href="https://zhuanlan.zhihu.com/p/657582060">折叠效果CoordinatorLayout</a></p>
 */
public class FixedBehavior extends AppBarLayout.Behavior {

    private static final int TYPE_FLING = 1;
    private boolean isFlinging;
    private boolean shouldBlockNestedScroll;

    public FixedBehavior(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    public boolean onInterceptTouchEvent(@NonNull CoordinatorLayout parent, @NonNull AppBarLayout child, MotionEvent ev) {
        shouldBlockNestedScroll = isFlinging;
        if (ev.getActionMasked() == MotionEvent.ACTION_DOWN) {//手指触摸屏幕的时候停止fling事件
            stopAppbarLayoutFling(child);
        }
        return super.onInterceptTouchEvent(parent, child, ev);
    }

    /**
     * 反射获取私有的flingRunnable 属性,考虑support 28以后变量名修改的问题
     * @return Field
     */
    private Field getFlingRunnableField() throws NoSuchFieldException {
        Class<?> superclass = this.getClass().getSuperclass();
        try {
            // support design 27及一下版本
            Class<?> headerBehaviorType = null;
            if (superclass != null) {
                headerBehaviorType = superclass.getSuperclass();
            }
            if (headerBehaviorType != null) {
                return headerBehaviorType.getDeclaredField("mFlingRunnable");
            }else {
                return null;
            }
        } catch (NoSuchFieldException e) {
            // 可能是28及以上版本
            Class<?> headerBehaviorType = superclass.getSuperclass().getSuperclass();
            if (headerBehaviorType != null) {
                return headerBehaviorType.getDeclaredField("flingRunnable");
            } else {
                return null;
            }
        }
    }

    /**
     * 反射获取私有的scroller 属性,考虑support 28以后变量名修改的问题
     * @return Field
     */
    private Field getScrollerField() throws NoSuchFieldException {
        Class<?> superclass = this.getClass().getSuperclass();
        try {
            // support design 27及一下版本
            Class<?> headerBehaviorType = null;
            if (superclass != null) {
                headerBehaviorType = superclass.getSuperclass();
            }
            if (headerBehaviorType != null) {
                return headerBehaviorType.getDeclaredField("mScroller");
            }else {
                return null;
            }
        } catch (NoSuchFieldException e) {
            // 可能是28及以上版本
            Class<?> headerBehaviorType = superclass.getSuperclass().getSuperclass();
            if (headerBehaviorType != null) {
                return headerBehaviorType.getDeclaredField("scroller");
            }else {
                return null;
            }
        }
    }

    /**
     * 停止appbarLayout的fling事件
     */
    private void stopAppbarLayoutFling(AppBarLayout appBarLayout) {
        try {
            Field flingRunnableField = getFlingRunnableField();
            Field scrollerField = getScrollerField();
            if (flingRunnableField != null) {
                flingRunnableField.setAccessible(true);
            }
            if (scrollerField != null) {
                scrollerField.setAccessible(true);
            }
            Runnable flingRunnable = null;
            if (flingRunnableField != null) {
                flingRunnable = (Runnable) flingRunnableField.get(this);
            }
            OverScroller overScroller = (OverScroller) scrollerField.get(this);
            if (flingRunnable != null) {
                appBarLayout.removeCallbacks(flingRunnable);
                flingRunnableField.set(this, null);
            }
            if (overScroller != null && !overScroller.isFinished()) {
                overScroller.abortAnimation();
            }
        } catch (NoSuchFieldException | IllegalAccessException e) {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onStartNestedScroll(
        @NonNull CoordinatorLayout parent, 
        @NonNull AppBarLayout child,
        @NonNull View directTargetChild, 
        View target,
        int nestedScrollAxes, 
        int type
    ) {
        stopAppbarLayoutFling(child);
        return super.onStartNestedScroll(parent, child, directTargetChild, target, nestedScrollAxes, type);
    }

    @Override
    public void onNestedPreScroll(CoordinatorLayout coordinatorLayout,
                                  @NonNull AppBarLayout child, View target,
                                  int dx, int dy, int[] consumed, int type) {
        if (type == TYPE_FLING) {
            isFlinging = true;
        }
        if (!shouldBlockNestedScroll) {
            super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
        }
    }

    @Override
    public void onNestedScroll(
        @NonNull CoordinatorLayout coordinatorLayout,
        @NonNull AppBarLayout child,
        @NonNull View target, 
        int dxConsumed, 
        int dyConsumed, 
        int dxUnconsumed, 
        int dyUnconsumed, 
        int type
    ) {
        if (!shouldBlockNestedScroll) {
            super.onNestedScroll(coordinatorLayout, child, target, dxConsumed,
                    dyConsumed, dxUnconsumed, dyUnconsumed, type);
        }
    }

    @Override
    public void onStopNestedScroll(
        CoordinatorLayout coordinatorLayout, 
        @NonNull AppBarLayout abl,
        View target, 
        int type
    ) {
        super.onStopNestedScroll(coordinatorLayout, abl, target, type);
        isFlinging = false;
        shouldBlockNestedScroll = false;
    }
}

注意事项

如果项目开了混淆,记得要对这个类取消混淆协议,因为:里面用了反射获取一些内容。

否则,会出现测试环境防抖动已经正常处理了,但是正式环境还是没有处理掉抖动问题。

  • 当时产品、UE、UI是没有这个需求的,项目组是不知道这回事的所以没有同步信息给到测试同学去增加测试用例,我是自己使用的时候觉得体验很不好然后加进去的这个优化。这个修复只有我知道,测试那边没有同步到这个问题。
  • 我是后面自己从Google Play Store下载了这个APP来使用,才知道我作为开发去完成的这个用户体验升级,并没有体现给到线上用户。
  • 于是,定位到了混淆的问题,然后修复了Release包的问题,在下一次的发版中这个问题才真正修复了。(小版本升级最好不要放在这种情况上面,一般来说还是伴随着产品制定的下一个大版本更新进行上线比较好。发版太多的话,测试团队、线上用户和以及应用分发商店都不太愿意)

Just something casual. Hope you like it. Built with VitePress