Search code examples
androidwebviewmaterial-designnestedscrollview

WebView inside NestedScrollView cannot be scrolled


I've got an activity that use WebView to load the web content. The problem arise when I want to implement Flexible Space with image. I can expand and collapse the Toolbar, but when the Toolbar already collapsed, The scrollbar stuck there. I can't scroll the content inside the WebView.

This is the XML:

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

    <WebView
        android:id="@+id/read_full_content_wv"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>


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

Do you have any solution for this?

Regards, Elmer


EDIT After I look at the link that given by LinX64. I tried to add:

public class FullContentActivity extends AppCompatActivity {
   ...
   @Override
   protected void onCreate(Bundle savedInstance){
      ...
      WebView webView = (WebView) findViewById(R.id.read_full_content_wv);
      webView.setWebViewClient(new MyBrowser());
      webView.loadData(extra.get(1).toString(), "text/html", "utf-8");
      ...
   }
   ...
   private class MyBrowser extends WebViewClient {
      @Override
      public void onPageStarted(WebView view, String url, Bitmap favicon){
         NestedScrollView nsv = (NestedScrollView) findViewById(R.id.nsv_fc);
         nsv.scrollTo(0,0);
      }
   }
}

and still got stucked.


EDIT 2 FYI: I tried on ASUS Zenfone 6 - KitKat 4.4.2 Is it possible KitKat can't load it correctly?


EDIT 3: The best solution I've got for this question

After working around, I think I cannot get experience I want for Flexible Space using CollapsingToolbarLayout combined with WebView, so I changed WebView to TextView and use Html.fromHtml() to read the content. Unless Google gonna update or fix some features so we can combine CollapsingToolbarLayout and WebView together.


Solution

  • Got this one working with changing the height attribute of the underlying Webview.

    For NestedScrollView use attribute

    android:fillViewport="true"
    

    and for WebView use android:layout_height="wrap_content"

    Full Answer here