Ale*_*dro 7 android scroll bounce smooth-scrolling android-webview
在 Pixel 3 和 Android Pie 上测试我的应用程序时,我发现我的应用程序中有一个 Webview 的奇怪滚动行为,向下或向上滚动时,滚动根本不流畅,有时它会跳回到原来的位置或完全跳转到顶部或底部。
This happens when I swipe up or down (dragging and letting it go)
Here is a quick look of the problem (I was trying to scroll down): https://imgur.com/a/8acgfCx
While scrolling down, it moved very slow, it moves so little, and then at the end the scroll jumps to the top a little bit itself.
This is an extended version, the jumps happens (randomly) when the scroll its about to stop and then jumps a little. https://imgur.com/a/K0N0utK
I was testing my app with a LG phone and Huawei phone -real devices- (Marshmallow and Nougat), didn't have any problem in any of these devices.
On the emulator I tested a Nexus 5 with Android API 23 (M) and didn't have any problem. But changed the OS on the Nexus 5 to Android Oreo and found the scrolling issue there too.
I thought that the problem was Android Oreo+... but then I tested on the emulator Pixel 3 with Android 23 (M), and have the same scrolling issue. So maybe the device has something to do, but the Android version too.
I've tried to change the layer type like this:
webView.setLayerType(View.LAYER_TYPE_HARDWARE, null)
but doesn't have any effect. the "hardwareAccelerated" tag is true in the Manifest.
The setup of the webview is pretty simple:
<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>
The settings are also pretty basic:
webView.settings.javaScriptEnabled = true
webView.settings.domStorageEnabled = true
webView.settings.databaseEnabled = true
webView.settings.cacheMode = LOAD_CACHE_ELSE_NETWORK
Could this be a hardware or android API issue?
小智 4
尝试像这样改变你的布局
<?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);
    }
| 归档时间: | 
 | 
| 查看次数: | 4427 次 | 
| 最近记录: |