Nestedscrollview内的Webview内容不能顺畅滚动

问题描述 投票:2回答:1

由于我在布局中需要一些动画和过渡,我在NestedScrollView中有一个Web视图。现在的问题是垂直滚动工作完全正常(我想这是由于嵌套滚动视图的滚动而不是Web视图),但水平滚动是平滑的。水平滚动,如webview内部内容的水平滚动(例如webview内的轮播)。

这是我的布局: -

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/frameLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/bg">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:id="@+id/coordinator"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <androidx.appcompat.widget.SearchView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="8dp"
                android:background="@color/white"
                app:layout_scrollFlags="scroll|enterAlways" />
        </com.google.android.material.appbar.AppBarLayout>

        <androidx.swiperefreshlayout.widget.SwipeRefreshLayout
            android:id="@+id/activity_main_swipe_refresh_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <androidx.core.widget.NestedScrollView
                android:id="@+id/nested_scroll_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" >

               <WebView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior" />
            </androidx.core.widget.NestedScrollView>
        </androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <fr.castorflex.android.smoothprogressbar.SmoothProgressBar
        android:id="@+id/progress_bar"
        android:layout_width="match_parent"
        android:layout_height="3.5dp"
        android:indeterminate="true"
        app:layout_constraintTop_toTopOf="parent"
        app:spb_color="#FB9043"
        app:spb_mirror_mode="false"
        app:spb_reversed="false"
        android:layout_marginTop="60dp"
        app:spb_sections_count="5"
        app:spb_speed="1.0"
        app:spb_stroke_separator_length="4.0dp"
        app:spb_stroke_width="4.0dp" />
</androidx.constraintlayout.widget.ConstraintLayout>

谁能帮我这个?

android webview android-coordinatorlayout android-nestedscrollview
1个回答
1
投票

在这个链接https://stackoverflow.com/a/34310846/6840443的帮助下,我得到了答案

所以他所做的是他基本上制作了一个自定义的NestedScrollView,并且覆盖了onInterceptTouchEvent方法并处理了它应该如何被消耗的触摸动作,所以当垂直滚动仍然由NestedScrollView处理时,水平滚动没有被处理然后,它传播到视图的子节点,恰好是已经处理这种滚动的webView。

这是它的代码: -

package com.smartprix.main

import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.ViewConfiguration
import androidx.core.widget.NestedScrollView


class SmartNestedScrollView : NestedScrollView {
    private var slop: Int = 0
    private val mInitialMotionX: Float = 0.toFloat()
    private val mInitialMotionY: Float = 0.toFloat()


    private var xDistance: Float = 0.toFloat()
    private var yDistance: Float = 0.toFloat()
    private var lastX: Float = 0.toFloat()
    private var lastY: Float = 0.toFloat()

    constructor(context: Context) : super(context) {
        init(context)
    }

    private fun init(context: Context) {
        val config = ViewConfiguration.get(context)
        slop = config.scaledEdgeSlop
    }

    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        init(context)
    }

    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        init(context)
    }

    override fun onInterceptTouchEvent(ev: MotionEvent): Boolean {
        val x = ev.x
        val y = ev.y
        when (ev.action) {
            MotionEvent.ACTION_DOWN -> {
                yDistance = 0f
                xDistance = yDistance
                lastX = ev.x
                lastY = ev.y

                // This is very important line that fixes
                computeScroll()
            }
            MotionEvent.ACTION_MOVE -> {
                val curX = ev.x
                val curY = ev.y
                xDistance += Math.abs(curX - lastX)
                yDistance += Math.abs(curY - lastY)
                lastX = curX
                lastY = curY

                if (xDistance > yDistance) {
                    return false
                }
            }
        }
        return super.onInterceptTouchEvent(ev)
    }
}```
© www.soinside.com 2019 - 2024. All rights reserved.