美好的资源android collectionandroid开发工作随笔

CoordinatorLayout和AppBarLayout 嵌

2018-06-06  本文已影响1413人  vb12

问题描述

先看出现抖动时的录屏:


android_coordinatorLayout_appbarlayout_bug.gif

如何复现:

只需要在Android Studio中按照谷歌官方模板, 生成一个Scrolling Activity:


图片.png

修改它的布局文件 activity_scrolling.xml 把Appbarlayout控件的高度设置的大一点, 这里使用380dp. 不修改也能复现这个问题, 但是高度值修改的大一点, 容易复现.
如下:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".ScrollingActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/app_bar"
        android:layout_width="match_parent"
        android:layout_height="380dp"
        android:fitsSystemWindows="true"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/toolbar_layout"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:toolbarId="@+id/toolbar">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <include layout="@layout/content_scrolling" />

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email" />

</android.support.design.widget.CoordinatorLayout>

编译, 运行:
显示:


图片.png

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

测试环境:

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support:design:27.1.1'
    implementation 'com.android.support:recyclerview-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.0.2'

解决方案

为AppBarLayout这只自定义的behavior: FixAppBarLayoutBehavior.java
这个类的具体代码如下:

public class FixAppBarLayoutBehavior extends AppBarLayout.Behavior {

    private static final String TAG = "AppBarLayoutBehavior";

    public FixAppBarLayoutBehavior() {
        super();
    }

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

    // --------------begin added by shaopx -------------

    @Override
    public boolean onInterceptTouchEvent(CoordinatorLayout parent, AppBarLayout child, MotionEvent ev) {
        if (ev.getAction() == ACTION_DOWN) {
            Object scroller = getSuperSuperField(this, "mScroller");
            if (scroller != null && scroller instanceof OverScroller) {
                OverScroller overScroller = (OverScroller) scroller;
                overScroller.abortAnimation();
            }
        }

        return super.onInterceptTouchEvent(parent, child, ev);
    }

    private Object getSuperSuperField(Object paramClass, String paramString) {
        Field field = null;
        Object object = null;
        try {
            field = paramClass.getClass().getSuperclass().getSuperclass().getDeclaredField(paramString);
            field.setAccessible(true);
            object = field.get(paramClass);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return object;
    }
    // --------------------------- end

    ...................  // 这里其他代码可以忽略, 是为了解决另一个问题的. 
}

其实这帖子我们只需要关心上面的那个方法:onInterceptTouchEvent() 在其中, 当检测到down事件时, 取消了mScroller的运行(如果它正在scroll的话).
这里因为要访问父类(其实是父类的父类)的 mScroller变量. 所以用到了一个反射的工具方法, 这个没什么可多说的.

个人感觉, 在appbarlayout滑动时, 必须有一个公开的接口, 或者方法能够停止它的fling滑动. 现在有吗? 反正我没找到, 就只能通过反射实现了

推荐阅读

至于appbarlayout的滑动原理, 推荐看另一篇博文:
https://blog.csdn.net/litefish/article/details/52588235

代码路径

详细代码已经放到github上: (运行示例时选择第一个谷歌示例)
https://github.com/shaopx/CoordinatorLayoutExample

上一篇下一篇

猜你喜欢

热点阅读