[WebView] - Coordinator layout 和 WebView

一、概述

我们知道, android-design-support-library 中 CoordinatorLayout提供了对子视图之间的触摸事件的额外级别的控制,这是设计库中许多组件利用的东西。比如floating action buttons、the app bar、Collapsing Toolbars。
其中CoordinatorLayout and the app bar涉及了引用程序栏和滚动技术,使用AppBarLayout允许您的工具栏或者其他视图(例如TabLayout提供的选项卡)对标记为ScrollingViewBehavior的兄弟视图中的滚动事件做出反应。

二、WebView 和 CoordinatorLayout滚动技术

首先呢,很遗憾的告诉你,WebView在谷歌的材料设计库中并不支持CoordinatorLayout滚动技术。
也就是说不支持:

<WebView
  android:id="@+id/webview"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

上述的代码没有任何效果。
如果想要Toolbar或者其他组件对WebView的滚动做出反应,则需要使用NestedScrollView

<android.support.v4.widget.NestedScrollView
    android:id="@+id/scrollView"
    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>

NestedScrollView

NestedScrollView is just like `ScrollView`, but it supports acting as both a nested scrolling parent and child on both new and old versions of Android. Nested scrolling is enabled by default.

NestedScrollView就像“ScrollView”一样,但它支持在新版本和旧版本的Android上同时作为嵌套滚动父级和子级。 默认情况下启用嵌套滚动。

这样子,当WebView做出Scroll动作的时候,ToolBar会做出一些你想要的响应。

三、NestedScrollView和WebView一起使用存在的问题

当你使用NestedScrollView和WebVie一起用的时候,虽然实现了CoordinatorLayout滚动效果,但是当WebView打开某个URL的时候,你的WebViewActivity会被Force stop问题,其logcat中描述是这样子的:

java.lang.IllegalStateException: Unable to create layer for WebView

原来,谷歌已经知道了这个issue,回复是不建议WebView和NestedScrollView一起使用!但是谷歌也不给出WebView可以和CoordinatorLayout一起使用的解决方案,坑!

那么经过一番激烈的问答:
https://code.google.com/p/android/issues/detail?id=198965

得到了解决方案,总结一下就是:

[1]. 原因分析:

If you put webview in a NestedScrollView, height is wrap_contents, and the webview expands to the size of the page. This is as if in desktop, you resize the browser window to height of the page so it can’t scroll vertically. The scrolling is happening in NestedScrollView rather than webview itself.
So a consequence of no js in the page sees any scroll events, so it doesn’t know you’ve scrolled to the end of the page load more content.

Also hardware layers is backed by GL textures, and they have a maximum size (which is least the screen size, although usually not much bigger). If a view becomes larger than the maximum texture size, then it won’t work with a hardware layer. This applies to any view, not just webview.

Solution: don’t put webview in a NestedScrollView. don’t use webview in wrap_contents mode. Let webview scroll the web page itself.

如果将webview放在NestedScrollView中,height是wrap_contents,并且webview扩展为页面的大小。 这就好像在桌面上一样,您可以将浏览器窗口的大小调整为页面的高度,因此无法垂直滚动。 滚动是在NestedScrollView而不是webview本身发生的。

所以页面中没有js的结果会看到任何滚动事件,所以它不知道你已经滚动到页面的末尾加载更多的内容。

硬件层也由GL纹理支持,并且它们具有最大尺寸(这是最小的屏幕尺寸,尽管通常不会大得多)。 如果视图变得大于最大纹理大小,那么它将不能与硬件层一起使用。 这适用于任何视图,而不仅仅是webview。

建议:不要将webview放在NestedScrollView中。 不要在wrap_contents模式下使用webview。 让webview滚动网页本身。

[2]. 解决方案:
自定义WebView:NestedScrollWebView.java
经过实践,这个解决方案行之有效。

最后吐槽一下,出了问题,百度一下,全都是一些WebVie滚动解决方案,无FUCK说!!!

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 6
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值