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

Android 如何阻止ScrollView在WebView中滚动到当前加载页面的底部,android,webview,scrollview,swiperefreshlayout,Android,Webview,Scrollview,Swiperefreshlayout,我知道存在类似的问题,我已经尝试并实施了所有这些解决方案建议。尽管WebView已经完成了数据加载,但ScrollView从未停止向下滚动。我想用onScrollListener使用pull刷新和隐藏操作栏。一切都很好,但我无法阻止scrolView不停地滚动到底部 我读到: 我也读过: 我试图实现一个自定义的scrollview,它覆盖requestChildFocus方法 我在我的xml文件中添加了android:genderantfocusability=“blocksDescendant

我知道存在类似的问题,我已经尝试并实施了所有这些解决方案建议。尽管WebView已经完成了数据加载,但ScrollView从未停止向下滚动。我想用
onScrollListener
使用pull刷新和隐藏操作栏。一切都很好,但我无法阻止
scrolView
不停地滚动到底部

我读到:

我也读过:

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

我在我的xml文件中添加了
android:genderantfocusability=“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 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>
这是我的xml文件:

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

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


顺便说一句,添加android:DegenantFocusability=“BlocksDescents”会造成问题比如一旦Google.com打开,你就无法在Google的搜索栏中输入任何内容

我也有同样的问题。我的解决方案是在加载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的父级。在我的例子中,您应该更改为相应的WebView