如何防止滚动视图在加载数据后滚动到webview?

时间:2012-03-23 16:01:03

标签: android android-layout android-webview android-viewpager android-scrollview

所以我有一个引人入胜的问题。尽管我不是手动或以编程方式滚动我的视图,但我的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。我该如何防止这种情况?

7 个答案:

答案 0 :(得分:264)

我遇到了同样的问题,经过几个小时的尝试后,最终对我有用的只是将descendantFocusability属性添加到包含LinearLayout的ScrollView,其值为blockDescendants。在你的情况下:

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

从那以后没有再出现问题:)

答案 1 :(得分:38)

你可以简单地将它添加到你的LinearLayout:android:focusableInTouchMode =“true”。它对我有用。

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

答案 2 :(得分:26)

您应该创建新的类扩展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。

答案 3 :(得分:5)

在主布局中添加这些行可以解决问题

android:descendantFocusability="blocksDescendants"

答案 4 :(得分: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" >

答案 5 :(得分:4)

可能有些人遇到了同样的问题,所以我会帮忙。

我试图在我的主ScrollView中输入 android:descendantFocusability =&#34; beforeDescendants&#34; ,如下所示:

rq-worker1

并且它不起作用,所以我不得不将RelativeLayout设为ScrollView的父级,并在父级中放置 android:descendantFocusability =&#34; beforeDescendants&#34; 以及。

所以我解决了以下问题:

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

答案 6 :(得分:0)

我必须使用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" >