Make AppBarLayout respond to scrolling in WebView

@Rick de Vries the simplest way without any third party library is that after AppBarLayout make a NestedScrollView having the child as WebView only. Below is an example for the same -

        </com.google.android.material.appbar.AppBarLayout>

    <androidx.core.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </androidx.core.widget.NestedScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

This worked for me.


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

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

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.

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


Things placed inside CoordinatorLayout must be designed and implemented to work with it or it will not coordinate with any other sibling views. But well ... Toolbar is not designed for that. AppBarLayout is just an component that is prepared to make Toolbar works perfectly with CoordinatorLayout.

LinearLayout is not designed to work with CoordinatorLayout.It is for more easy, you just need to add an attribute to the LinearLayout telling its scroll behavior

<LinearLayout
...
app:layout_behavior="@string/appbar_scrolling_view_behavior"
...
>

you can try make AppBarLayout respond to the WebView by this

app:layout_behavior="@string/appbar_scrolling_view_behavior"

since ScrollView is now a direct child of CoordinatorLayout.

<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    ...
</LinearLayout>

ScrollView was not designed to work with CoordinatorLayout (again). You need to use the another one, NestedScrollView, provided in Android Support Library v4, which is designed to work with CoordinatorLayout since born.

And with the same reason, please note that the classic ListView doesn't work with CoordinatorLayout as well. Only RecyclerView works.