如何在数据加载后防止ScrollView滚动到WebView?

110

我有一个有趣的问题。尽管我没有手动或以编程方式滚动视图,但在加载完其中的数据后,我的WebView会被自动滚动到视图中。

我在viewpager中有一个片段。当我首次加载页面时,它按预期工作并显示所有内容。但一旦我“翻转页面”,数据加载完成后WebView会弹回到页面顶部,隐藏其上方的视图,这是不希望看到的。

有人知道如何防止发生这种情况吗?

我的布局如下:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/article_title"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="2dp"
            android:text="Some Title"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="@color/article_title"
            android:textStyle="bold" />

        <LinearLayout
            android:id="@+id/LL_Seperator"
            android:layout_width="fill_parent"
            android:layout_height="1dp"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:background="@color/text"
            android:orientation="horizontal" >
        </LinearLayout>

        <WebView
            android:id="@+id/article_content"
            android:layout_width="match_parent"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/article_link"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="5dp"
            android:layout_marginTop="5dp"
            android:layout_marginRight="10dp"
            android:layout_marginLeft="10dp"
            android:text="View Full Article"
            android:textColor="@color/article_title"
            android:textStyle="bold" />
    </LinearLayout>

</ScrollView>

我也没有把重点放在任何事情上。默认情况下,似乎在WebView加载完成后会自动滚动到它。如何防止这种情况发生?


为什么你需要ScrollView,因为WebView本身就是可滚动的? - znat
为了提高速度和质量,将某些方面保持在Web视图之外,而不是将它们呈现为HTML。 - Navarr
1
mjp66的回答最好 - 也许你应该接受他的回答。 - AndrewS
8个回答

269

我也遇到了同样的问题,试了几个想法数小时后,最终对我有效的方法就是在ScrollView包含的LinearLayout上添加descendantFocusability属性,值为blocksDescendants。在您的情况中:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants" >

自那以后,问题再也没有发生过。


3
这会对这些控件的可访问性产生负面影响吗? - Cory Trese
37
请注意,如果存在任何子视图,如EditText,则此方法将使它们无法编辑。 - djhworld
2
当你搜索“ScrollView Webview autoscroll”这些词时,这个问题/答案并不会出现,但它应该出现。也许这个评论可以帮上忙!我使用WebViewFragment时遇到了同样的问题,在发布了自己的问题(现在已删除)之前,我经过了大量的搜索,仍然找不到这个问题。 - Colin M.
1
在一个Xamarin.Android项目中工作时,遇到了在带有多个GridView的ScrollView中自动滚动的问题,这些GridView是异步填充的,其中包含下载的图像。这个解决方案非常完美,以至于我一开始很难相信它,并不得不再次测试它。 - YumeYume
1
RecyclerView中,广告也会出现同样的问题。你的解决方案同样适用 :) - Minas Mina
显示剩余3条评论

44

您可以简单地将此添加到LinearLayout中:android:focusableInTouchMode="true"。这对我有效。

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >

4
无副作用的工作! - Vaibhav Jani

27

您应该创建一个继承ScrollView的新类,然后覆盖requestChildFocus方法:

public class MyScrollView extends ScrollView {

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

然后在您的XML布局中使用:

<MyScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background" >

对我来说可以。ScrollView将不再自动滚动到WebView。


5
还需要构造函数: `public ExtendedScrollView(Context context) { super(context); } public ExtendedScrollView( Context context, AttributeSet attributeSet) { super(context, attributeSet); } public ExtendedScrollView( Context context, AttributeSet attributeSet, int defStyle) { super(context, attributeSet, defStyle); }` - Erik B

6
在主布局中添加这些行可以解决问题。
android:descendantFocusability="blocksDescendants"

4

就像这样:

<com.ya.test.view.MyScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:descendantFocusability="beforeDescendants"
        android:focusable="true"
        android:focusableInTouchMode="true" >

4

可能有些人遇到了我曾经遇到的问题,所以我来帮忙解决一下。

我试图在我的主要ScrollView中添加以下android:descendantFocusability="beforeDescendants"

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants">
    /*my linearlayout or whatever to hold the views */

由于某些原因,它没有起作用,所以我不得不将RelativeLayout设置为ScrollView的父级,并在父级中加入android:descendantFocusability="beforeDescendants"

因此,我通过以下方式解决了这个问题:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants">

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">
/*my linearlayout or whatever to hold the views */

0
我在我的ScrollView中添加了android:overScrollMode="never",并将高度设置为wrap_content
我的视图非常复杂,因为它是由LinearLayout内嵌套LinearLayout组成的遗留代码。
这对我很有帮助,希望也能帮到其他人!

-2

我必须使用MyScrollView的完全限定名称,否则会出现膨胀异常。

<com.mypackagename.MyScrollView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/background" >

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