Android Webview在点击链接时滚动到顶部

7
我有一个Webview在我的应用程序中加载静态HTML内容。
问题是,当我点击链接或嵌入式内容(推文、YouTube视频)时,webview会向上滚动回到顶部。
这似乎是一个JS问题,如此处所述。
我尝试使用NestedScrollView或一些参数技巧,例如在LinearLayout父元素中使用 android:focusableInTouchMode="true" 但没有任何效果。
这是我的XML:
  <android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <WebView
      android:id="@+id/article_webview"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"/>
  </android.support.v4.widget.NestedScrollView>

这将会在一个回收视图中显示。

我的问题是:

是否有一种方法可以在webview设置或xml中防止这种行为发生?

谢谢!


嘿,保罗,你找到解决方案了吗? - Emmanuel Loisance
嘿,保罗,你找到解决方案了吗? - Denny
2个回答

8

我遇到了同样的问题。我的WebView也在一个RecyclerView里。

当我在RecyclerView上添加了android:descendantFocusability="blocksDescendants"后,这个问题就不再出现了。

<androidx.recyclerview.widget.RecyclerView
  android:descendantFocusability="blocksDescendants"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"/>

使用 WebView 时无法获取焦点,输入字段不可编辑。 - Aura Lee

2

我也遇到了同样的问题。我的 WebView 在 LinearLayout 中。

当我在 LinearLayout 上添加了 android:descendantFocusability="blocksDescendants" 后,问题得到了解决。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接