WebView不滚动

发布于 2025-02-09 22:27:18 字数 1849 浏览 1 评论 0 原文

我在屏幕上的NestedScrollview中有一个WebView。另外,我在屏幕下方有recyclerview,当recyclerview关闭WebView时,我向上滚动以再次查看WebView,那时WebView的内容停止了滚动。我如何解决?

            <androidx.core.widget.NestedScrollView
            android:id="@+id/webScrollContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:visibility="@{playerType == PlayerViewType.WEB ? View.VISIBLE : View.GONE}"
            tools:visibility="gone">

            <WebView
                android:id="@+id/previewWebView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:isScrollContainer="false"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:layout_scrollFlags="scroll|exitUntilCollapsed" />

        </androidx.core.widget.NestedScrollView>




    @SuppressLint("SetJavaScriptEnabled", "JavascriptInterface")
private fun initWebView() {
    web.let {
        with(it.settings) {
            javaScriptEnabled = true
            userAgentString =
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.117 Safari/537.36"
            domStorageEnabled = true
            loadWithOverviewMode = true
            useWideViewPort = true
            mediaPlaybackRequiresUserGesture = true
            allowFileAccess = true
            cacheMode = WebSettings.LOAD_DEFAULT
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            it.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        }
        it.webChromeClient = WebChromeProgressClient()
        it.webViewClient = WebClient()
    }
}

I have a webview in NestedScrollView on screen. Also, I have recyclerview below the screen, and when recyclerview items close the webview, i scroll up to see the webview again, at that point the content of the webview stops scrolling. How I can fix it?

            <androidx.core.widget.NestedScrollView
            android:id="@+id/webScrollContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:visibility="@{playerType == PlayerViewType.WEB ? View.VISIBLE : View.GONE}"
            tools:visibility="gone">

            <WebView
                android:id="@+id/previewWebView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"
                android:isScrollContainer="false"
                app:layout_behavior="@string/appbar_scrolling_view_behavior"
                app:layout_scrollFlags="scroll|exitUntilCollapsed" />

        </androidx.core.widget.NestedScrollView>




    @SuppressLint("SetJavaScriptEnabled", "JavascriptInterface")
private fun initWebView() {
    web.let {
        with(it.settings) {
            javaScriptEnabled = true
            userAgentString =
                "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.117 Safari/537.36"
            domStorageEnabled = true
            loadWithOverviewMode = true
            useWideViewPort = true
            mediaPlaybackRequiresUserGesture = true
            allowFileAccess = true
            cacheMode = WebSettings.LOAD_DEFAULT
        }
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            it.setLayerType(View.LAYER_TYPE_HARDWARE, null)
        }
        it.webChromeClient = WebChromeProgressClient()
        it.webViewClient = WebClient()
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对你而言 2025-02-16 22:27:18
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.webkit.WebView
import androidx.core.view.MotionEventCompat
import androidx.core.view.NestedScrollingChild
import androidx.core.view.NestedScrollingChildHelper
import androidx.core.view.ViewCompat


class NestedWebView(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
    WebView(context!!, attrs, defStyleAttr), NestedScrollingChild {
    private var mLastY = 0
    private val mScrollOffset = IntArray(2)
    private val mScrollConsumed = IntArray(2)
    private var mNestedOffsetY = 0
    private val mChildHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)

    constructor(context: Context?) : this(context, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(
        context,
        attrs,
        R.attr.webViewStyle
    ) {
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        var returnValue = false
        val event = MotionEvent.obtain(ev)
        val action = MotionEventCompat.getActionMasked(event)
        if (action == MotionEvent.ACTION_DOWN) {
            mNestedOffsetY = 0
        }
        val eventY = event.y.toInt()
        event.offsetLocation(0f, mNestedOffsetY.toFloat())
        when (action) {
            MotionEvent.ACTION_MOVE -> {
                var totalScrollOffset = 0
                var deltaY = mLastY - eventY
                // NestedPreScroll
                if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
                    totalScrollOffset += mScrollOffset[1]
                    deltaY -= mScrollConsumed[1]
                    event.offsetLocation(0f, (-mScrollOffset[1]).toFloat())
                    mNestedOffsetY += mScrollOffset[1]
                }
                returnValue = super.onTouchEvent(event)

                // NestedScroll
                if (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) {
                    totalScrollOffset += mScrollOffset[1]
                    event.offsetLocation(0f, mScrollOffset[1].toFloat())
                    mNestedOffsetY += mScrollOffset[1]
                    mLastY -= mScrollOffset[1]
                }
                mLastY = eventY - totalScrollOffset
            }
            MotionEvent.ACTION_DOWN -> {
                returnValue = super.onTouchEvent(event)
                mLastY = eventY
                // start NestedScroll
                startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL)
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                returnValue = super.onTouchEvent(event)
                // end NestedScroll
                stopNestedScroll()
            }
        }
        return returnValue
    }

    // Nested Scroll implements
    override fun setNestedScrollingEnabled(enabled: Boolean) {
        mChildHelper.isNestedScrollingEnabled = enabled
    }

    override fun isNestedScrollingEnabled(): Boolean {
        return mChildHelper.isNestedScrollingEnabled
    }

    override fun startNestedScroll(axes: Int): Boolean {
        return mChildHelper.startNestedScroll(axes)
    }

    override fun stopNestedScroll() {
        mChildHelper.stopNestedScroll()
    }

    override fun hasNestedScrollingParent(): Boolean {
        return mChildHelper.hasNestedScrollingParent()
    }

    override fun dispatchNestedScroll(
        dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int,
        offsetInWindow: IntArray?
    ): Boolean {
        return mChildHelper.dispatchNestedScroll(
            dxConsumed,
            dyConsumed,
            dxUnconsumed,
            dyUnconsumed,
            offsetInWindow
        )
    }

    override fun dispatchNestedPreScroll(
        dx: Int,
        dy: Int,
        consumed: IntArray?,
        offsetInWindow: IntArray?
    ): Boolean {
        return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)
    }

    override fun dispatchNestedFling(
        velocityX: Float,
        velocityY: Float,
        consumed: Boolean
    ): Boolean {
        return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed)
    }

    override fun dispatchNestedPreFling(velocityX: Float, velocityY: Float): Boolean {
        return mChildHelper.dispatchNestedPreFling(velocityX, velocityY)
    }

    init {
        isNestedScrollingEnabled = true
    }
}

在您的布局中:

<com.nestedscrollwebviewexample.NestedWebView
    android:id="@+id/nested_webview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:fillViewport="true"
    android:focusable="true"
    android:isScrollContainer="false"
    android:visibility="visible"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout_scrollFlags="scroll|exitUntilCollapsed" />
import android.content.Context
import android.util.AttributeSet
import android.view.MotionEvent
import android.webkit.WebView
import androidx.core.view.MotionEventCompat
import androidx.core.view.NestedScrollingChild
import androidx.core.view.NestedScrollingChildHelper
import androidx.core.view.ViewCompat


class NestedWebView(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :
    WebView(context!!, attrs, defStyleAttr), NestedScrollingChild {
    private var mLastY = 0
    private val mScrollOffset = IntArray(2)
    private val mScrollConsumed = IntArray(2)
    private var mNestedOffsetY = 0
    private val mChildHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)

    constructor(context: Context?) : this(context, null) {}
    constructor(context: Context?, attrs: AttributeSet?) : this(
        context,
        attrs,
        R.attr.webViewStyle
    ) {
    }

    override fun onTouchEvent(ev: MotionEvent): Boolean {
        var returnValue = false
        val event = MotionEvent.obtain(ev)
        val action = MotionEventCompat.getActionMasked(event)
        if (action == MotionEvent.ACTION_DOWN) {
            mNestedOffsetY = 0
        }
        val eventY = event.y.toInt()
        event.offsetLocation(0f, mNestedOffsetY.toFloat())
        when (action) {
            MotionEvent.ACTION_MOVE -> {
                var totalScrollOffset = 0
                var deltaY = mLastY - eventY
                // NestedPreScroll
                if (dispatchNestedPreScroll(0, deltaY, mScrollConsumed, mScrollOffset)) {
                    totalScrollOffset += mScrollOffset[1]
                    deltaY -= mScrollConsumed[1]
                    event.offsetLocation(0f, (-mScrollOffset[1]).toFloat())
                    mNestedOffsetY += mScrollOffset[1]
                }
                returnValue = super.onTouchEvent(event)

                // NestedScroll
                if (dispatchNestedScroll(0, mScrollOffset[1], 0, deltaY, mScrollOffset)) {
                    totalScrollOffset += mScrollOffset[1]
                    event.offsetLocation(0f, mScrollOffset[1].toFloat())
                    mNestedOffsetY += mScrollOffset[1]
                    mLastY -= mScrollOffset[1]
                }
                mLastY = eventY - totalScrollOffset
            }
            MotionEvent.ACTION_DOWN -> {
                returnValue = super.onTouchEvent(event)
                mLastY = eventY
                // start NestedScroll
                startNestedScroll(ViewCompat.SCROLL_AXIS_VERTICAL)
            }
            MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL -> {
                returnValue = super.onTouchEvent(event)
                // end NestedScroll
                stopNestedScroll()
            }
        }
        return returnValue
    }

    // Nested Scroll implements
    override fun setNestedScrollingEnabled(enabled: Boolean) {
        mChildHelper.isNestedScrollingEnabled = enabled
    }

    override fun isNestedScrollingEnabled(): Boolean {
        return mChildHelper.isNestedScrollingEnabled
    }

    override fun startNestedScroll(axes: Int): Boolean {
        return mChildHelper.startNestedScroll(axes)
    }

    override fun stopNestedScroll() {
        mChildHelper.stopNestedScroll()
    }

    override fun hasNestedScrollingParent(): Boolean {
        return mChildHelper.hasNestedScrollingParent()
    }

    override fun dispatchNestedScroll(
        dxConsumed: Int, dyConsumed: Int, dxUnconsumed: Int, dyUnconsumed: Int,
        offsetInWindow: IntArray?
    ): Boolean {
        return mChildHelper.dispatchNestedScroll(
            dxConsumed,
            dyConsumed,
            dxUnconsumed,
            dyUnconsumed,
            offsetInWindow
        )
    }

    override fun dispatchNestedPreScroll(
        dx: Int,
        dy: Int,
        consumed: IntArray?,
        offsetInWindow: IntArray?
    ): Boolean {
        return mChildHelper.dispatchNestedPreScroll(dx, dy, consumed, offsetInWindow)
    }

    override fun dispatchNestedFling(
        velocityX: Float,
        velocityY: Float,
        consumed: Boolean
    ): Boolean {
        return mChildHelper.dispatchNestedFling(velocityX, velocityY, consumed)
    }

    override fun dispatchNestedPreFling(velocityX: Float, velocityY: Float): Boolean {
        return mChildHelper.dispatchNestedPreFling(velocityX, velocityY)
    }

    init {
        isNestedScrollingEnabled = true
    }
}

In your Layout:

<com.nestedscrollwebviewexample.NestedWebView
    android:id="@+id/nested_webview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#000000"
    android:fillViewport="true"
    android:focusable="true"
    android:isScrollContainer="false"
    android:visibility="visible"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    app:layout_scrollFlags="scroll|exitUntilCollapsed" />
妄断弥空 2025-02-16 22:27:18

问题是由 WebView nestedscrollview

recyclerveive a href =“ https://developer.android.com/reference/android/webkit/webview” rel =“ nofollow noreferrer”> webview ,但在最终中“ https://developer.android.com/reference/android/webkit/webview” rel =“ nofollow noreferrer”> webview 不再滚动。


创建一个 custom nestedwebview class class /a>要实现a nestedscrollingscrollingchild interface 。它应该覆盖 ontouchevent()方法

修复此设置 android:nestedScrollingEnabled =“ true” for NestedScrollview 。这将允许 webview href =“ https://developer.android.com/reference/androidx/core/widget/nestedscrollview” rel =“ nofollow noreferrer”> nestedscrollview

<androidx.core.widget.NestedScrollView
    android:id="@+id/"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport=""
    android:visibility=""
    tools:visibility="gone"
    android:nestedScrollingEnabled="true">

我还要做的是禁用硬件的加速度。尝试调用 it.set.set.setlayertype(view.layer_type_software,nul) ,而不是 it.setlayerType(view。 layer_type_hardware,nul)


设置 webview “ https://developer.android.com/reference/kotlin/androidx/core/widget/nestedscrollview” rel =“ nofollow noreferrer”> nastedScrollingEnabled >。它将禁用滚动。将其添加到您的 initwebview()fun

it.isNestedScrollingEnabled = false

之前对其进行测试,因为它可能会影响您的性能。


最后一个解决方案是使用自定义WebView 实现 /代码>

.kt文件

class NestedWebView(context: Context, attrX: AttributeX) : WebView(context, attrX), NestedScrollingChild {
    //private val childHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)

    init {
        //example: isNestedScrollingEnabled = true
    }

    override fun onTouchEvent(: ): Boolean {
    }

    override fun setNestedScrollingEnabled(enabled: Boolean) {
        //childHelper.isNestedScrollingEnabled = enabled
    }

    override fun isNestedScrollingEnabled(): Boolean {
        //return childHelper.isNestedScrollingEnabled
    }

    override fun startNestedScroll(axes: Int): Boolean {
        //return childHelper.startNestedScroll(axes)
    }

    override fun stopNestedScroll() {
        //childHelper.stopNestedScroll()
    }

    override fun hasNestedScrollingParent(): Boolean {
        //return childHelper.hasNestedScrollingParent()
    }

        //dispatchNestedScroll
        //dispatchPreScroll
        //dispatchNestedFling
        //dispatchNestedPreFling

    }

}

.xml布局

<com.example.myapp.NestedWebView
    android:id="@+id/"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

初始化并使用自定义 nestedwebview 用reg webview

The issue is caused by WebView that is inside of a NestedScrollView.

The RecyclerView covers the WebView but in the final the WebView isn't scrolling anymore.


Create a custom NestedWebView Class to implement a NestedScrollingChild Interface. It should override onTouchEvent() Method.

Fix this setting android:nestedScrollingEnabled="true" for NestedScrollView. This will allow WebView to scroll within NestedScrollView.

<androidx.core.widget.NestedScrollView
    android:id="@+id/"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport=""
    android:visibility=""
    tools:visibility="gone"
    android:nestedScrollingEnabled="true">

What I would additionally do is to disable the acceleration of hardware. Try to call it.setLayerType(View.LAYER_TYPE_SOFTWARE, nul), instead of it.setLayerType(View.LAYER_TYPE_HARDWARE, nul).


Set the WebView's NastedScrollingEnabled property false. It will disable scrolling. Add this in your initWebView() fun :

it.isNestedScrollingEnabled = false

Test it before because it may affect your performance.


The last solution is the usage of custom WebView to implement NestedScrollingChild interface.

.kt file

class NestedWebView(context: Context, attrX: AttributeX) : WebView(context, attrX), NestedScrollingChild {
    //private val childHelper: NestedScrollingChildHelper = NestedScrollingChildHelper(this)

    init {
        //example: isNestedScrollingEnabled = true
    }

    override fun onTouchEvent(: ): Boolean {
    }

    override fun setNestedScrollingEnabled(enabled: Boolean) {
        //childHelper.isNestedScrollingEnabled = enabled
    }

    override fun isNestedScrollingEnabled(): Boolean {
        //return childHelper.isNestedScrollingEnabled
    }

    override fun startNestedScroll(axes: Int): Boolean {
        //return childHelper.startNestedScroll(axes)
    }

    override fun stopNestedScroll() {
        //childHelper.stopNestedScroll()
    }

    override fun hasNestedScrollingParent(): Boolean {
        //return childHelper.hasNestedScrollingParent()
    }

        //dispatchNestedScroll
        //dispatchPreScroll
        //dispatchNestedFling
        //dispatchNestedPreFling

    }

}

.XML layout

<com.example.myapp.NestedWebView
    android:id="@+id/"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Initialize and use the custom NestedWebView instance with reg WebView.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文