协调器布局行为不起作用

Coordinator Layout Behavior Not Working

我试图在 Webview 向上滚动时隐藏工具栏,而当 webview 向下滚动时显示。为了实现这种行为,我发现了一种叫做 Coordinator Layout 的东西。但不幸的是它不适合我。 我的 xml 长得怎么样?

<CoordinatorLayout
    -------

    <AppBarLayout
       -------
        <Toolbar
           -----
            app:layout_scrollFlags="scroll|enterAlways"
           ------
            />
    </AppBarLayout>

    <SwipeRefreshLayout
       -------------
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <WebView
            android:id="@+id/globalWebView"
            ---------------------------
            ---------------------------/>
    </SwipeRefreshLayout>

</CoordinatorLayout>

实际上,当我发现它不起作用时,我尝试了其他方式,即

<CoordinatorLayout>

    <AppBarLayout>
        <Toolbar....
            app:layout_scrollFlags="scroll|enterAlways"/>
    </AppBarLayout>
        <WebView...
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</CoordinatorLayout>

但这对我不起作用。有什么解决方案可以让它发挥作用吗? TIA.

将您的 webView 包装成 NestedScrollView 如下所示..

<CoordinatorLayout
    ....>
    <AppBarLayout>
        <Toolbar....
            app:layout_scrollFlags="scroll|enterAlways"/>
    </AppBarLayout>
    <NestedScrollView
        ....
        ...
        app:layout_behavior="@string/appbar_scrolling_view_behavior">
        <WebView....
            ... />
    </NestedScrollView>
</CoordinatorLayout>

此外,您可以根据需要使用 nestedScrollingEnabled(boolean)NestedScrollView