11

I have a problem with that :

<?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"
    android:id="@+id/coordinatorLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appBarLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:layout_scrollFlags="scroll|enterAlways" />
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.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="match_parent" />
    </android.support.v4.widget.NestedScrollView>

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

When I scrolls in the webview, the toolbar is hidden or shown (perfect !) but there is a problem with the loading / positioning web pages. For example, if I scrolls to the middle of a page and I click on a link, the new page that will load also be located at approximately the middle of the page instead of on top. As if scrollbars were not moving from one page to another.

if I add to the NestedScrollView:

android:fillViewport="true"

everything works with the webview (pages load and appear well although starting from the top) but I lose the Hide/Show with the toolbar :(

Do you have any idea about this problem?

Thank you in advance for your help :)

(For information : Android Design Support Library : 23.0.1)

Yop

Yop
  • 111
  • 1
  • 5

1 Answers1

4

My assumption: Since you are putting the WebView inside a NestedScrollView the scrolling is not done on the WebView level so when you load a new page the NestedScrollView stays in the same position.

Suggestion: create a WebViewClient and override onPageStarted here you should change the NestedScrollView scroll position to 0:

nestedScrollView.scrollTo(0, 0);
Raanan
  • 4,777
  • 27
  • 47
  • 1
    Yes, for me this works for scrolling to the top, another issue is when jumping web content from tall height webview to another webview which is small content, it has long white space scroll to the bottom. Can you suggest for fixing this? Thanks. – Jawaad Nov 18 '15 at 06:29
  • 1
    @Jawaad it would be better if you open a seperate question for your issue, provide code sample and describe what you tried already. As for your question: I would use the Hierarchy Viewer to check if the issue is the WebView not re-sizing or the NestedScrollView. What you could try is in onPageFinished to call invalidate on the NestedScrollView, not sure if this will solve the issue. – Raanan Nov 18 '15 at 11:26
  • This is great idea, but another problem is the zoom control, it doesn't work in this layout setup. – Sam Chen Feb 20 '20 at 16:01