Vassiliy Vorobyov Vassiliy Vorobyov - 2 months ago 23
Android Question

WebView inside ViewPager inside NestedScrollView not scrolling vertically

I need to enable webview scrolling that placed into viewpager that is in nestescrollview that is necessary because I'm using CollapsingToolbarLayout. But I cant scroll it. All works except WebView scrolling. Where I am wrong?

enter image description here

Here is my activity markup:

<?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:id="@+id/activity_read_article"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorBackgroundLight"
android:fitsSystemWindows="true"
tools:context="ru.pwrg.revolution.activities.ReadArticleActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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="wrap_content"
android:fitsSystemWindows="true"
app:contentScrim="@color/colorBackgroundDark"
app:layout_scrollFlags="scroll|exitUntilCollapsed">

<ImageView
android:id="@+id/article_cover"
android:layout_width="match_parent"
android:layout_height="200dp"
app:srcCompat="@color/colorBackgroundDark" />

</android.support.design.widget.CollapsingToolbarLayout>

<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/colorPrimaryDark" />


</android.support.design.widget.AppBarLayout>
<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">

<android.support.v4.view.ViewPager
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

</android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>


WebView fragment for ViewPager markup:

<WebView android:id="@+id/article_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp"
xmlns:android="http://schemas.android.com/apk/res/android" />

Answer Source

Wrap your WebView into a NestedScrollView:

<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<WebView android:id="@+id/article_webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="8dp" />

</android.support.v4.widget.NestedScrollView>

NestedScrollView works together with CoordinatorLayout and knows how to handle scrolls correctly.