WebView和AppBarLayout嵌套滑动联动无效分析及解决办法

本文分析了WebView与AppBarLayout嵌套滑动联动无效的问题,并提供了详细的解决方案。通过实现嵌套滑动接口和重写相关方法,解决了WebView在惯性滑动时无法联动AppBarLayout的问题。示例源码展示了具体实现。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

WebView和AppBarLayout嵌套滑动联动无效分析及解决办法

目录

序章

上两篇博客分析了嵌套滑动,CoordinatorLayoutBehavior,这次来结合两者分析.

如果尚未了解嵌套滑动NestedScroll的使用及CoordinatorLayout与Behavior的使用,请先移步查看

引题

Android CoordinatorLayout和Behavior解析一篇中讲完了Behavior的依赖联动实现部分,本篇将结合实例讲解Behavior的嵌套滑动联动部分.

Materials Design中AppBarLayout嵌套滑动的基本实现.

用Android Studio可以直接新建一个Scrolling Activity

运行创建的例子

这是一个很简单的AppBarLayout嵌套滑动的例子.

查看其布局如下

activity_scrolling.xml

<?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="@dimen/app_bar_height"
        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_margin="@dimen/fab_margin"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"
        app:srcCompat="@android:drawable/ic_dialog_email"
        android:elevation="50dp"
        />

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

AppBarLayout的子View中添加app:layout_scrollFlags属性,这里的子View是CollapsingToolbarLayout,关于CollapsingToolbarLayout这是一个配合AppBarLayoutToolBar实现伸缩的布局,在此不多做介绍,有兴趣请自行百度喽.

对于scrollFlags有如下属性

        /**
         * The view will be scroll in direct relation to scroll events. This flag needs to be
         * set for any of the other flags to take effect. If any sibling views
         * before this one do not have this flag, then this value has no effect.
         */
        public static final int SCROLL_FLAG_SCROLL = 0x1;

        /**
         * When exiting (scrolling off screen) the view will be scrolled until it is
         * 'collapsed'. The collapsed height is defined by the view's minimum height.
         *
         * @see ViewCompat#getMinimumHeight(View)
         * @see View#setMinimumHeight(int)
         */
        public static final int SCROLL_FLAG_EXIT_UNTIL_COLLAPSED = 0x2;

        /**
         * When entering (scrolling on screen) the view will scroll on any downwards
         * scroll event, regardless of whether the scrolling view is also scrolling. This
         * is commonly referred to as the 'quick return' pattern.
         */
        public static final int SCROLL_FLAG_ENTER_ALWAYS = 0x4;

        /**
         * An additional flag for 'enterAlways' which modifies the returning view to
         * only initially scroll back to it's collapsed height. Once the scrolling view has
         * reached the end of it's scroll range, the remainder of this view will be scrolled
         * into view. The collapsed height is defined by the view's minimum height.
         *
         * @see ViewCompat#getMinimumHeight(View)
         * @see View#setMinimumHeight(int)
         */
        public static final int SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED = 0x8;

        /**
         * Upon a scroll ending, if the view is only partially visible then it will be snapped
         * and scrolled to it's closest edge. For example, if the view only has it's bottom 25%
         * displayed, it will be scrolled off screen completely. Conversely, if it's bottom 75%
         * is visible then it will be scrolled fully into view.
         */
        public static final int SCROLL_FLAG_SNAP = 0x10;

对着英文注释强行解释一波哈哈

源码里的Flag xml中layout_scrollFlags对应的字段 说明
SCROLL_FLAG_SCROLL scroll 滑动支持所必须得属性,如果没有这个属性则不可滑动.若在该属性所附属的View的前一个View(它的哥哥或者姐姐…)没有这个属性,则该属性所附属的View也将不支持滑动
SCROLL_FLAG_EXIT_UNTIL_COLLAPSED exitUntilCollapsed 当主体滑动的View上滑时,该属性会让所附属的View的高度压缩到最小高度.
SCROLL_FLAG_ENTER_ALWAYS enterAlways 只要主体滑动的View有下滑操作,不管其是否有滑动到顶部,都会把当前属性所附属的View拉下来.
SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED enterAlwaysCollapsed
评论 13
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值