Nestedscrollview中的Webview内容无法平滑滚动

时间:2019-03-16 06:51:04

标签: android webview android-coordinatorlayout android-nestedscrollview

由于我想要在布局中进行某些动画处理和转换,因此我在NestedScrollView中拥有一个Web视图。现在,问题是垂直滚动工作得非常好(我猜这是由于嵌套滚动视图的滚动而不是Web视图的滚动),而水平滚动是平滑的。水平滚动与Web视图内内容的水平滚动一样(例如,Web视图内的轮播)。

这是我的布局:-

<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>

有人可以帮我吗?

1 个答案:

答案 0 :(得分: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)
    }
}```