如何阻止ScrollView在WebView中滚动到当前加载页面的底部

4

我知道有类似的问题,我已经尝试并实现了所有的解决方案,但是它们都不能解决我的问题。即使WebView加载数据完成,ScrollView仍然不停地向底部滚动。我想使用下拉刷新并使用onScrollListener隐藏操作栏。一切都很好,但我无法阻止scrollView不停地滚动到底部。

我阅读了这篇文章:如何防止ScrollView在数据加载后滚动到WebView?

我也阅读了这篇文章:WebView强制将我的ScrollView滚动到底部

我尝试实现一个自定义的scrollView,它重写了requestChildFocus方法。

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

我也添加了android:focusableInTouchMode="true",但还是没有用,没有改变。它仍然强制滚动到底部。

这是我的自定义scrollView:

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的搜索栏中输入任何内容!
1个回答

0

我遇到了同样的问题。我的解决方案是在加载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的父级,您应该更改为相应的父级。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接