CoordinatorLayout滑动抖动问题

简介: 目录介绍 01.CoordinatorLayout滑动抖动问题描述 02.滑动抖动问题分析 03.自定义AppBarLayout.Behavior说明 04.CoordinatorLayout滑动抖动解决方案 05.

目录介绍

  • 01.CoordinatorLayout滑动抖动问题描述
  • 02.滑动抖动问题分析
  • 03.自定义AppBarLayout.Behavior说明
  • 04.CoordinatorLayout滑动抖动解决方案
  • 05.案例测试是否根本问题

好消息

  • 博客笔记大汇总【16年3月到至今】,包括Java基础及深入知识点,Android技术博客,Python学习笔记等等,还包括平时开发中遇到的bug汇总,当然也在工作之余收集了大量的面试题,长期更新维护并且修正,持续完善……开源的文件是markdown格式的!同时也开源了生活博客,从12年起,积累共计N篇[近100万字,陆续搬到网上],转载请注明出处,谢谢!
  • 链接地址:https://github.com/yangchong211/YCBlogs
  • 如果觉得好,可以star一下,谢谢!当然也欢迎提出建议,万事起于忽微,量变引起质变!

01.CoordinatorLayout滑动抖动问题描述

  • 先看下布局

    <android.support.design.widget.CoordinatorLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
    
        <android.support.design.widget.AppBarLayout
            android:id="@+id/appbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
    
            <!--这个是滚动头部-->
            <include layout="@layout/include_find_header"/>
    
            <!--这个是吸顶布局-->
            <include layout="@layout/include_sticky_header"/>
    
        </android.support.design.widget.AppBarLayout>
    
        <!--app:layout_behavior 属性,该布局包含一个竖直方向的RecyclerView-->
        <include layout="@layout/include_recycler_view" />
    
    </android.support.design.widget.CoordinatorLayout>
  • 出现的问题

    • 用手指轻轻滑动CoordinatorLayout部分, 上滑, 快速抬起手指, 形成一个fling操作。其实就是向上滑动一下!
    • 这时, 整个CoordinatorLayout部分会向上移动(fling),在停止移动之前,在下面的区域(也就是xml布局中的include_recycler_view)来一个反向的滑动(fling) , 这时整个页面就会开始或大或小的抖动, 非常明显。

02.滑动抖动问题分析

  • CoordinatorLayout向上fling滚动无法被外部中断

    • CoordinatorLayout和子View的联动时通过CoordinatorLayout.Behavior实现的,AppBarLayout使用的Behavior继承了HeaderBehavior。
    • 问题就在这里。HeaderBehavior的onTouchEvent中使用Scroller实现了fling操作,但是没有通过NestedScrolling API对外开放,也就说一旦HeaderBehavior的fling动作形成,无法由外部主动中断。
  • RecyclerView向下fling滚动

    • 与AppBarLayout同层级的RecyclerView可以通过升级过的NestedScrolling API对AppBarLayout产生影响,比如RecyclerView向下fling时滑动到item 0之后,如果AppBarLayout可以滑动时会给AppBarLayout施加一个同样向下的fling动作,以此形成一个连贯的下滑fling。
    • 那么问题来了。当HeaderBehavior产生的向上的fling没有结束时,RecyclerView又送来向下的fling,抖动就产生了。
  • 分析一下HeaderBehavior

    • 当检测到down事件时, 取消了mScroller的运行(如果它正在scroll的话)。这里因为要访问父类(其实是父类的父类)的 mScroller变量。
    • 然后通过反射拿到mScroller变量,在onInterceptTouchEvent拦截事件中,当手指离开的时候,则停止overScroller动画效果。
    • 然后通过反射拿到flingRunnable变量,在onInterceptTouchEvent拦截事件中,当手指离开的时候,则需要remove所有的flingRunnable。

03.自定义AppBarLayout.Behavior说明

  • AppBarLayout简单说明

    • AppBarLayout是一个vertical的LinearLayout,实现了很多material的概念,主要是跟滑动相关的。AppBarLayout的子view需要提供layout_scrollFlags参数。AppBarLayout和CoordinatorLayout强相关,一般作为CoordinatorLayout的子类,配套使用。
      按我的理解,AppBarLayout内部有2种view,一种可滑出(屏幕),另一种不可滑出,根据app:layout_scrollFlags区分。一般上边放可滑出的下边放不可滑出的。
  • AppBarLayout.Behavior部分方法说明

    • onInterceptTouchEvent():是否拦截触摸事件
    • onTouchEvent():处理触摸事件
    • layoutDependsOn():确定使用Behavior的View要依赖的View的类型
    • onDependentViewChanged():当被依赖的View状态改变时回调
    • onDependentViewRemoved():当被依赖的View移除时回调
    • onMeasureChild():测量使用Behavior的View尺寸
    • onLayoutChild():确定使用Behavior的View位置
    • onStartNestedScroll():嵌套滑动开始(ACTION_DOWN),确定Behavior是否要监听此次事件
    • onStopNestedScroll():嵌套滑动结束(ACTION_UP或ACTION_CANCEL)
    • onNestedScroll():嵌套滑动进行中,要监听的子 View的滑动事件已经被消费
    • onNestedPreScroll():嵌套滑动进行中,要监听的子 View将要滑动,滑动事件即将被消费(但最终被谁消费,可以通过代码控制)
    • onNestedFling():要监听的子 View在快速滑动中
    • onNestedPreFling():要监听的子View即将快速滑动

04.CoordinatorLayout滑动抖动解决

  • 通过反射拿到flingRunnable变量,注意这里我判断了一下27和28版本的问题,27及以下是mFlingRunnable,28及以上是flingRunnable,一定要注意这个问题。

    /**
     * 反射获取私有的flingRunnable 属性,考虑support 28以后变量名修改的问题
     * @return Field
     * @throws NoSuchFieldException
     */
    private Field getFlingRunnableField() throws NoSuchFieldException {
        Class<?> superclass = this.getClass().getSuperclass();
        try {
            // support design 27及一下版本
            Class<?> headerBehaviorType = null;
            if (superclass != null) {
                String name = superclass.getName();
                LogUtil.d("AppBarLayout.Behavior父类",name);
                headerBehaviorType = superclass.getSuperclass();
            }
            if (headerBehaviorType != null) {
                String name = headerBehaviorType.getName();
                LogUtil.d("AppBarLayout.Behavior父类的父类1",name);
                return headerBehaviorType.getDeclaredField("mFlingRunnable");
            }else {
                return null;
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
            // 可能是28及以上版本
            Class<?> headerBehaviorType = superclass.getSuperclass().getSuperclass();
            if (headerBehaviorType != null) {
                String name = headerBehaviorType.getName();
                LogUtil.d("AppBarLayout.Behavior父类的父类2",name);
                return headerBehaviorType.getDeclaredField("flingRunnable");
            } else {
                return null;
            }
        }
    }
  • 通过反射拿到scroller变量,和上面类似。

    /**
     * 反射获取私有的scroller 属性,考虑support 28以后变量名修改的问题
     * @return Field
     * @throws NoSuchFieldException
     */
    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) {
            e.printStackTrace();
            // 可能是28及以上版本
            Class<?> headerBehaviorType = superclass.getSuperclass().getSuperclass();
            if (headerBehaviorType != null) {
                return headerBehaviorType.getDeclaredField("scroller");
            }else {
                return null;
            }
        }
    }
  • 然后在onInterceptTouchEvent拦截事件里处理逻辑。当手指触摸屏幕的时候停止fling事件

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
        LogUtil.d(TAG, "onInterceptTouchEvent:" + child.getTotalScrollRange());
        shouldBlockNestedScroll = isFlinging;
        switch (ev.getActionMasked()) {
            case MotionEvent.ACTION_DOWN:
                //手指触摸屏幕的时候停止fling事件
                stopAppbarLayoutFling(child);
                break;
            default:
                break;
        }
        return super.onInterceptTouchEvent(parent, child, ev);
    }
    
    /**
     * 停止appbarLayout的fling事件
     * @param appBarLayout
     */
    private void stopAppbarLayoutFling(AppBarLayout appBarLayout) {
        //通过反射拿到HeaderBehavior中的flingRunnable变量
        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 = null;
            if (scrollerField != null) {
                overScroller = (OverScroller) scrollerField.get(this);
            }
            //下面是关键点
            if (flingRunnable != null) {
                LogUtil.d(TAG, "存在flingRunnable");
                appBarLayout.removeCallbacks(flingRunnable);
                flingRunnableField.set(this, null);
            }
            if (overScroller != null && !overScroller.isFinished()) {
                overScroller.abortAnimation();
            }
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
    }
  • 完整版本的代码如下所示

    /**
     * <pre>
     *     @author yangchong
     *     blog  : https://github.com/yangchong211
     *     time  : 2019/03/13
     *     desc  : 自定义Behavior
     *     revise: 解决appbarLayout若干问题
     *              1)快速滑动appbarLayout会出现回弹
     *              2)快速滑动appbarLayout到折叠状态下,立马下滑,会出现抖动的问题
     *              3)滑动appbarLayout,无法通过手指按下让其停止滑动
     * </pre>
     */
    public class AppBarLayoutBehavior extends AppBarLayout.Behavior {
    
        private static final String TAG = "AppbarLayoutBehavior";
        private static final int TYPE_FLING = 1;
        private boolean isFlinging;
        private boolean shouldBlockNestedScroll;
    
        public AppBarLayoutBehavior(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
            LogUtil.d(TAG, "onInterceptTouchEvent:" + child.getTotalScrollRange());
            shouldBlockNestedScroll = isFlinging;
            switch (ev.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    //手指触摸屏幕的时候停止fling事件
                    stopAppbarLayoutFling(child);
                    break;
                default:
                    break;
            }
            return super.onInterceptTouchEvent(parent, child, ev);
        }
    
        /**
         * 反射获取私有的flingRunnable 属性,考虑support 28以后变量名修改的问题
         * @return Field
         * @throws NoSuchFieldException
         */
        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) {
                e.printStackTrace();
                // 可能是28及以上版本
                Class<?> headerBehaviorType = superclass.getSuperclass().getSuperclass();
                if (headerBehaviorType != null) {
                    return headerBehaviorType.getDeclaredField("flingRunnable");
                } else {
                    return null;
                }
            }
        }
    
        /**
         * 反射获取私有的scroller 属性,考虑support 28以后变量名修改的问题
         * @return Field
         * @throws NoSuchFieldException
         */
        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) {
                e.printStackTrace();
                // 可能是28及以上版本
                Class<?> headerBehaviorType = superclass.getSuperclass().getSuperclass();
                if (headerBehaviorType != null) {
                    return headerBehaviorType.getDeclaredField("scroller");
                }else {
                    return null;
                }
            }
        }
    
        /**
         * 停止appbarLayout的fling事件
         * @param appBarLayout
         */
        private void stopAppbarLayoutFling(AppBarLayout appBarLayout) {
            //通过反射拿到HeaderBehavior中的flingRunnable变量
            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) {
                    LogUtil.d(TAG, "存在flingRunnable");
                    appBarLayout.removeCallbacks(flingRunnable);
                    flingRunnableField.set(this, null);
                }
                if (overScroller != null && !overScroller.isFinished()) {
                    overScroller.abortAnimation();
                }
            } catch (NoSuchFieldException e) {
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                e.printStackTrace();
            }
        }
    
        @Override
        public boolean onStartNestedScroll(CoordinatorLayout parent, AppBarLayout child,
                                           View directTargetChild, View target,
                                           int nestedScrollAxes, int type) {
            LogUtil.d(TAG, "onStartNestedScroll");
            stopAppbarLayoutFling(child);
            return super.onStartNestedScroll(parent, child, directTargetChild, target,
                    nestedScrollAxes, type);
        }
    
        @Override
        public void onNestedPreScroll(CoordinatorLayout coordinatorLayout,
                                      AppBarLayout child, View target,
                                      int dx, int dy, int[] consumed, int type) {
            LogUtil.d(TAG, "onNestedPreScroll:" + child.getTotalScrollRange()
                    + " ,dx:" + dx + " ,dy:" + dy + " ,type:" + type);
            //type返回1时,表示当前target处于非touch的滑动,
            //该bug的引起是因为appbar在滑动时,CoordinatorLayout内的实现NestedScrollingChild2接口的滑动
            //子类还未结束其自身的fling
            //所以这里监听子类的非touch时的滑动,然后block掉滑动事件传递给AppBarLayout
            if (type == TYPE_FLING) {
                isFlinging = true;
            }
            if (!shouldBlockNestedScroll) {
                super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed, type);
            }
        }
    
        @Override
        public void onNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child,
                                   View target, int dxConsumed, int dyConsumed, int
                dxUnconsumed, int dyUnconsumed, int type) {
            LogUtil.d(TAG, "onNestedScroll: target:" + target.getClass() + " ,"
                    + child.getTotalScrollRange() + " ,dxConsumed:"
                    + dxConsumed + " ,dyConsumed:" + dyConsumed + " " + ",type:" + type);
            if (!shouldBlockNestedScroll) {
                super.onNestedScroll(coordinatorLayout, child, target, dxConsumed,
                        dyConsumed, dxUnconsumed, dyUnconsumed, type);
            }
        }
    
        @Override
        public void onStopNestedScroll(CoordinatorLayout coordinatorLayout, AppBarLayout abl,
                                       View target, int type) {
            LogUtil.d(TAG, "onStopNestedScroll");
            super.onStopNestedScroll(coordinatorLayout, abl, target, type);
            isFlinging = false;
            shouldBlockNestedScroll = false;
        }
    
        private static class LogUtil{
            static void d(String tag, String string){
                Log.d(tag,string);
            }
        }
    
    }

05.案例测试是否根本问题

  • 代码如下所示

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        app:layout_behavior="org.yczbj.ycrefreshview.sticky.AppBarLayoutBehavior"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
  • 发现最终解决问题

06.参考案例

其他介绍

01.关于博客汇总链接

项目地址:https://github.com/yangchong211/YCRefreshView

目录
相关文章
|
12月前
|
Android开发 iOS开发 UED
Android webView 实现阻尼回弹效果
iOS webView默认滑动到顶部或者底部的时候,还可以继续通过手指拉扯滑动,松手后回弹;而Android webView默认是不行的,要实现跟iOS一样的效果,就需要自定义webView。
357 0
|
11月前
|
XML API Android开发
CoordinatorLayout与滚动的处理
CoordinatorLayout与滚动的处理
76 0
|
XML Android开发 数据格式
Android ScrollView嵌套RecyclerView滑动卡顿
Android ScrollView嵌套RecyclerView滑动卡顿
269 0
|
Android开发
RecycleView 不显示、显示不全及滑动卡顿
RecycleView 出现的不显示或显示不全。ScrollView中嵌套RecycleView滑动出现卡顿。
535 0
|
开发者 异构计算
RecyclerView滑动时卡顿怎么办?
RecyclerView滑动时卡顿怎么办?
465 0
RecyclerView滑动时卡顿怎么办?
|
Android开发 容器 Java
Android取消RecyclerView、ListView、ScrollView、HorizontalScrollView滑动到边缘闪现灰白色水波纹动画
Android取消RecyclerView、ListView、ScrollView、HorizontalScrollView滑动到边缘闪现灰白色水波纹动画 标准的Android RecyclerView、ListView、ScrollView、HorizontalScrollView滑动到边缘,会闪现灰白色水波纹动画,以这样大的动画效果提示用户已经滑动到边缘,没法再滑动了。
2924 0
|
Android开发
解决ScrollView嵌套RecyclerView滑动卡顿问题
目前 RecyclerView 的使用已经比较普遍了,当需要嵌套使用 RecyclerView 和 ScrollView 时候滑动卡顿的问题,也遇到的越来越多,特此记录下。
2175 0