美文网首页
NestedScrollView WebView 使用的那些事

NestedScrollView WebView 使用的那些事

作者: androidfan | 来源:发表于2022-09-27 17:25 被阅读0次

google 已经不建议使用NestedScrollView 嵌套Webview 来使用了,但是实际上,很多小伙伴还是遇到各种奇葩的需求和产品,迫不得已还是会这样嵌套,但是肯定会遇到很多情况,接和我的实际,以及遇到的情况,我总结一下几点和解决方法,方便记忆也给大家提供一个思路:

1.ANR 问题

很有可能在页面更加载完毕就会出来“程序无响应”的ANR提示,然后就是黑屏,这个时候一定不要着急不要慌,可以debug看程序走的步骤,如果程序进行中遇到了Exception,也是会导致ANR的,把异常解决了就可以,如果debug没有发现问题,那么就需要看ANR日志了,至于收集方法,可以看我往期博客,具体命令是 adb bugreport

2.webview内容点击切换时,NestdScorllview 上下滑动页面

这个问题,困扰了我一上午,百度出来的都是要重写webview,但是也没有用,后来我发现,锁定父布局的焦点可以解决这个事情,但是,仅仅锁定一层还不行,还需要在根布局上也锁定,这样才能解决页面上下滑动的问题



    <com.xuexiang.xui.widget.XUIObservableScrollView
        android:id="@+id/nestedView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/white_f0f1f5"
        android:orientation="vertical"
        android:overScrollMode="never"
        android:scrollbars="none"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/pointsRecode">

        <LinearLayout
            android:layout_width="match_parent"
            android:descendantFocusability="blocksDescendants"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginLeft="@dimen/dp_8"
                android:layout_marginTop="@dimen/dp_10"
                android:elevation="3dp"
                android:translationZ="@dimen/dp_3"
                android:layout_marginRight="@dimen/dp_8"
                android:background="@drawable/layout_shape_white"
                android:padding="@dimen/dp_5">

                <WebView
                    android:id="@+id/mWebView"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content" />
                </LinearLayout>
        </LinearLayout>
    </com.xuexiang.xui.widget.XUIObservableScrollView>

最重要的就是android:descendantFocusability="blocksDescendants" 这句话了

3.webview底部留白的问题

当你解决完上面两个问题后,很可能发现webview的底部布局正常页面下面还多出来一块空白页面,说不要紧吧,肯定影响页面美观,所以还是要解决的。

  mWebView.webViewClient = object : WebViewClient() {
            //加载完成的时候会回调
            override fun onPageFinished(webView: WebView, s: String) {
                val params = mWebView.layoutParams
                params.width = resources.displayMetrics.widthPixels.minus(SizeUtils.dp2px(30f))
                params.height = mWebView.height - nestedView.height
                mWebView.layoutParams = params
            }
        }

动态获取webview的高度重新设置,就可以解决这个问题,另外在网页内容切换,有改变内容视图高度的时候,也要主动去重新设置webview的高度,这样才可以解决底部空白的问题。

今天先这样,快放假了,在坚持一下

相关文章

网友评论

      本文标题:NestedScrollView WebView 使用的那些事

      本文链接:https://www.haomeiwen.com/subject/mzafartx.html