【问题标题】:How to stop ScrollView from scrolling through to bottom of the current loaded page in WebView如何阻止 ScrollView 在 WebView 中滚动到当前加载页面的底部
【发布时间】:2025-12-21 20:15:06
【问题描述】:

我知道有类似的问题,我已经尝试并实施了所有这些解决方案建议。然而,他们都没有和我一起工作。即使 WebView 完成加载数据,ScrollView 也永远不会停止滚动到底部。我想使用拉来刷新和隐藏带有onScrollListener 的操作栏。一切都很好,但我无法阻止scrolView 不停地滚动到底部。

我读到这个:How to prevent a scrollview from scrolling to a webview after data is loaded?

我也读过这个:webview is forcing my scrollview to the bottom

我尝试实现一个自定义的scrollview,它会覆盖requestChildFocus 方法。

我在我的 xml 文件中添加了android:descendantFocusability="blocksDescendants"

我也添加了android:focusableInTouchMode="true",但还是不行,没有变化。它仍然强制永远滚动。

这是我的自定义滚动视图:

package com.example.john.cosmicbrowser;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.webkit.WebView;
import android.widget.ScrollView;

public class MyScrollView extends ScrollView
{
    public MyScrollView(Context context)
    {
        super(context);
    }

    public MyScrollView(Context context, AttributeSet attributeSet)
    {
        super(context, attributeSet);
    }

    public MyScrollView(Context context, AttributeSet attributeSet, int defStyle)
    {
        super(context, attributeSet, defStyle);
    }

    @Override
    public void requestChildFocus(View child, View focused)
    {
        if (focused instanceof WebView)
            return;
        super.requestChildFocus(child, focused);
    }
}

这是我的 xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout
    android:id="@+id/swipe"
    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"
    android:descendantFocusability="blocksDescendants"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context=".WebActivity"
    tools:showIn="@layout/activity_webview"
    >


    <com.example.john.cosmicbrowser.MyScrollView
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:focusableInTouchMode="true"
        android:descendantFocusability="blocksDescendants"
        >

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:descendantFocusability="blocksDescendants"
            >

            <WebView
                android:id="@+id/webView"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:padding="0dp"/>

            <ProgressBar
                android:id="@+id/cosmicProgressBar"
                style="?android:attr/progressBarStyleHorizontal"
                android:layout_width="match_parent"
                android:layout_height="4dp"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true"
                android:indeterminate="false"/>

        </RelativeLayout>
    </com.example.john.cosmicbrowser.MyScrollView>


</android.support.v4.widget.SwipeRefreshLayout>

例如,假设您在此 web 视图上打开了 Google.com。页面加载后,它难以置信地向下滚动到底部,您甚至在页面底部都看不到任何信息。我该如何解决这个问题?任何帮助将不胜感激!提前致谢!

顺便添加android:descendantFocusability="blocksDescendants"导致问题,比如一旦打开 Google.com 就无法搜索,您无法在 Google 的搜索栏中输入任何内容!

【问题讨论】:

    标签: android webview scrollview swiperefreshlayout


    【解决方案1】:

    我遇到了同样的问题。我的解决方案是在加载 URL 后立即将 webview 的高度设置为其内容:

    mWebView.setWebViewClient(new WebViewClient(){
                    @Override
                    public void onPageFinished(WebView view, String url) {
                        super.onPageFinished(view, url);
                        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mWebView.getMeasuredHeight());
                        params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
                        mWebView.setLayoutParams(params);
    
                    }
                });
    

    请注意,在我的情况下,RelativeLayout 是 WebView 的父级,您应该更改为相应的。

    【讨论】: