108

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

我在查看器中有一个片段。当我第一次加载寻呼机时,它按预期工作并显示了所有内容。但是,一旦我“翻转页面”,数据就会加载,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。我该如何防止这种情况?

4

7 回答 7

267

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

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

从那以后就没有再出现问题了。

于 2013-01-31T18:17:32.863 回答
43

您可以简单地将其添加到您的 LinearLayout:android:focusableInTouchMode="true"。这个对我有用。

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >
于 2013-10-02T09:23:10.183 回答
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。

于 2012-07-02T06:02:21.043 回答
5

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

android:descendantFocusability="blocksDescendants"
于 2014-06-04T19:30:57.787 回答
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" >
于 2015-05-21T06:34:37.770 回答
4

可能有人和我有同样的问题,所以我会帮忙的。

我试图将android:descendantFocusability="beforeDescendants"放在我的主 ScrollView 中,如下所示:

<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 */
于 2016-12-23T14:21:06.967 回答
-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" >
于 2012-12-30T18:57:13.113 回答