WebView和AppBarLayout嵌套滑动联动无效分析及解决办法
目录
序章
上两篇博客分析了嵌套滑动,CoordinatorLayout
与Behavior
,这次来结合两者分析.
如果尚未了解嵌套滑动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
这是一个配合AppBarLayout
和ToolBar
实现伸缩的布局,在此不多做介绍,有兴趣请自行百度喽.
对于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 |