gpt4 book ai didi

Android WebView 滚动不流畅并跳转/有回调

转载 作者:行者123 更新时间:2023-12-03 14:38:48 38 4
gpt4 key购买 nike

在 Pixel 3 和 Android Pie 上测试我的应用程序时,我发现我的应用程序中的 Webview 出现奇怪的滚动行为,向下或向上滚动时,滚动一点也不流畅,有时它会跳回原来的位置或完全跳动到顶部或底部。

当我向上或向下滑动(拖动并放开)时会发生这种情况

这是问题的快速浏览(我试图向下滚动):
https://imgur.com/a/8acgfCx

向下滚动时,它移动得很慢,它移动得很少,然后滚动到最后一点点自己跳到顶部。

这是一个扩展版本,当滚动即将停止然后跳跃一点时,跳跃(随机)发生。
https://imgur.com/a/K0N0utK

我正在使用 LG 手机和华为手机 - 真实设备 - (棉花糖和牛轧糖)测试我的应用程序,在这些设备中没有任何问题。

在模拟器上,我用 Android API 23 (M) 测试了 Nexus 5,没有任何问题。但是将 Nexus 5 上的操作系统更改为 Android Oreo 并在那里也发现了滚动问题。

我认为问题出在 Android Oreo+... 但后来我在模拟器 Pixel 3 和 Android 23 (M) 上进行了测试,并且遇到了同样的滚动问题。所以也许设备有事可做,但安卓版本也有。

我尝试像这样更改图层类型:

webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)

但没有任何效果。 list 中的“hardwareAccelerated”标签为真。

webview 的设置非常简单:
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".destinations.common.WebViewDestFragment" android:id="@+id/root_layout">
<WebView
android:id="@+id/webView"
android:layout_width="0dp"
android:layout_height="0dp"
android:overScrollMode="never"
android:scrollbars="vertical"
android:fadeScrollbars="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
</WebView>
</androidx.constraintlayout.widget.ConstraintLayout>

设置也很基本:
webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
webView.settings.databaseEnabled = true
webView.settings.cacheMode = LOAD_CACHE_ELSE_NETWORK

这可能是硬件或 android API 问题吗?

最佳答案

尝试像这样更改您的布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">

<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:overScrollMode="never"/>

</android.support.constraint.ConstraintLayout>

并将其添加到您的 java 文件中
if (Build.VERSION.SDK_INT >= 21) {
webview.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
}

//FOR WEBPAGE SLOW UI
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
webview.setLayerType(View.LAYER_TYPE_HARDWARE, null);
} else {
webview.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
}

关于Android WebView 滚动不流畅并跳转/有回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57473469/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com