使AppBarLayout响应WebView中的滚动

Ric*_*ies 13 xml android webview

在设计支持库(来自I/O)中,Google推出了AppBarLayout.当用户在片段中的WebView中滚动网站时,我正在尝试使工具栏动画出屏幕.当我将我的WebView元素放在NestedScrollView中时它确实有效,但这会使滚动浏览WebView错误并使得缩放变得不可能.如何让AppBarLayout响应(向下滚动时动画,在向上滚动时返回,就像在NestedScrollView中一样)到WebView?这些是我的XML文件:主要布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="@+id/main_coordinatorlayout">
<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
    android:id="@+id/appBarLayout">
    <android.support.v7.widget.Toolbar
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        android:id="@+id/toolbar"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/Theme.AppCompat.Light"
        app:layout_scrollFlags="scroll|enterAlways"/>
    <android.support.design.widget.TabLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/tabLayout"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/viewPager"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:layout_anchor="@id/viewPager"
    app:layout_anchorGravity="bottom|right|end"
    app:borderWidth="0dp"
    android:layout_margin="16dp"
    android:src="@drawable/ic_action_autorenew" />
Run Code Online (Sandbox Code Playgroud)

包含webview的片段代码:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/webView"
    android:layout_gravity="center_horizontal"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
Run Code Online (Sandbox Code Playgroud)

提前致谢!

Ric*_*ies 13

感谢@ torque203提供这个答案.我正在重新发布这个以吸引更多关注它.

您可以扩展webview并实现NestedScrollingChild.例如,您可以使用此Github项目中NestedWebView对象.

从那以后,您可以像支持嵌套滚动的普通视图一样,就像RecyclerView:

<your.package.name.NestedWebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
Run Code Online (Sandbox Code Playgroud)

在我的用例中,这很好用.

再次,@ torque203找到这个Github项目.

  • 当我使用 `CollapsingToolbarLayout` 测试 NestedWebView 时,它似乎无法正常工作。最严重的问题是双指缩放根本无法正常工作;每次捏合缩放后,它都会跳到页面上的不同点。其他问题是应用栏滚动动画断断续续,并且该类似乎无法正确跟踪嵌套滚动调度的原点。(我测试了支持 23.2.0 和 23.3.0。) (2认同)