使AppBarLayout响应WebView中的滚动 [英] Make AppBarLayout respond to scrolling in WebView

查看:77
本文介绍了使AppBarLayout响应WebView中的滚动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设计支持库(来自I/O)中,Google引入了AppBarLayout.当用户在Fragment的WebView中浏览网站时,我试图使工具栏在屏幕外进行动画处理.当我将WebView元素放入NestedScrollView时,它确实起作用,但是这使滚动WebView越野车成为不可能,并且无法进行缩放.如何使AppBarLayout对WebView响应(向下滚动时动画,向上滚动时返回,就像NestedScrollView中一样)?这些是我的XML文件:主要布局:

In the design support Library (from I/O) Google introduced the AppBarLayout. I'm trying to make the Toolbar animate out of the screen when the user scrolls through a website in a WebView in a Fragment. It does work when I put my WebView element inside a NestedScrollView, but that makes scrolling through the WebView buggy and makes pinch-to-zoom impossible. How can I make the AppBarLayout respond (animate away when scrolling down, return when scrolling up, just like in NestedScrollView) to the WebView? These are my XML files: The main layout:

<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" />

包含网页视图的片段代码:

The code of my fragment containing the 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" />

预先感谢!

推荐答案

感谢@ torque203 提供了此答案.我将其重新发布以引起更多关注.

Thanks to @torque203 for providing this answer. I'm reposting this to gather more attention to it.

您可以扩展Web视图并实现NestedScrollingChild.例如,您可以使用 NestedWebView 对象"rel =" nofollow noreferrer>此Github项目.

You can extend webview and implement NestedScrollingChild. For example, you can use the NestedWebView object found in this Github project.

从那时起,您可以像一个支持嵌套滚动的普通视图一样操作,例如RecyclerView:

From there on, you can act like it's a normal View that supports Nested Scrolling, like a RecyclerView:

<your.package.name.NestedWebView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior" />

在我的用例中,这很好.

In my use case, this worked fine.

再次感谢@ torque203 ,以找到此Github项目.

Again, credits to @torque203 for finding this Github project.

这篇关于使AppBarLayout响应WebView中的滚动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆