Android WebView 文明踩坑之路

情景一

webview 以头布局的形式添加到 RecyclerView 中,第一次进入页面,当页面中有 EditText 并且点击时,甚至是类似点赞更换图片、点击 WebView 任意区域,都会造成 WebView 自动滑动到最顶部;

分析:我的 WebView 在 onPageFinish() 的时候进行重新测量了高度,所以点击事件等很可能是 WebView 获取焦点重新绘制了,故而想到不让WebView获取焦点,尝试过使用

        android:focusable="true"
        android:focusableInTouchMode="true

上述代码只能解决 WebView 区域以外的点击造成的WebView滑动至顶部问题,第一次进入 WebView 时点击依然会造成 WebView 滚动;最后想到了 code android:descendantFocusability="blocksDescendants",貌似完美解决

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:descendantFocusability="blocksDescendants">
            <android.support.v7.widget.RecyclerView
                android:layout_width="match_parent"
                android:layout_height="match_parent"/>
    </LinearLayout>

descendantFocusability 属性说明:

    beforeDescendants:viewgroup会优先其子类控件而获取到焦点
    afterDescendants:viewgroup只有当其子类控件不需要获取焦点时才获取焦点
    blocksDescendants:viewgroup会覆盖子类控件而直接获得焦点

情景二

Android 5.0 + 部分手机上 WebView 加载 GIF图时 会造成 WebView 不停的闪烁

解决方法查看我的另一篇文章:WebView 5.0+闪烁以及白屏问题完美解决

posted @ 2018-02-09 10:54  SiberiaDante  阅读(1054)  评论(0编辑  收藏  举报