在 Listview 中滚动 webview 在 android 中不起作用

scrolling a webview inside a Listview won't work in android

我有一个在 listView 中显示多个 webView 的应用程序。我的问题是当我尝试上下滚动 webView 时,webView 不响应我的手指,除非我只使用 3 个手指。当它响应时,它几乎不会上下移动。我发现问题是 listview 禁止 webView 滚动。我试过使用:

listView.setEnabled(false);

但它对我不起作用。谁能告诉我如何解决这个问题? 这就是我在 listView 中添加 webView 布局的方式。

ArrayList<DefaultCards> cardsList = new ArrayList<>();
    cardsList.add(new DefaultCards("Hello Google","https://www.google.com", new WebViewClient()));
    CardsAdapter cardsAdapter = new CardsAdapter(this, cardsList);
    ListView listView = findViewById(R.id.cards_listview);
    listView.setAdapter(cardsAdapter);
    listView.setEnabled(false);

这是将显示 listView 的布局:

<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/cards_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

最后,这是将插入到 listView 中的布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:background="@color/colorBackground"
android:padding="5dp">

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    card_view:cardCornerRadius="4dp">

    <WebView
        android:id="@+id/webView"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_marginTop="50dp"/>


</android.support.v7.widget.CardView>

对此持保留态度,但您可能想尝试为您的列表持有者使用回收器视图,为您的 web 视图容器使用嵌套滚动视图。

此外,如果 webview 的内容大到足以滚动,您可能需要将 isScrollContainer="true" 属性添加到 webview 的 xml.

同样,这只是一个大胆的猜测。希望对您有所帮助!

列表视图正在拦截滚动。您可以通过创建实现 NestedScrollingChild、NestedScrollingParent 的 NestedScrollingWebView 来防止这种情况。

如果您搜索嵌套滚动 Webview,则有很多代码示例。 使用它而不是常规的 Webview。