首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在加载数据后防止滚动视图滚动到webview?

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

Stack Overflow用户
提问于 2012-03-24 00:01:03
回答 6查看 36.7K关注 0票数 108

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

我在取景器里找到了一个碎片。当我第一次加载寻呼机时,它按预期工作,所有内容都会显示出来。但是,一旦我“翻转页面”,数据加载和WebView弹出到页面的顶部,隐藏它上面的视图,这是不可取的。

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

我的布局是这样的:

代码语言:javascript
复制
<?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。我如何防止这种情况发生?

EN

回答 6

Stack Overflow用户

回答已采纳

发布于 2013-10-02 17:23:10

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

代码语言:javascript
复制
 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >
票数 43
EN

Stack Overflow用户

发布于 2013-02-01 02:17:33

我也遇到了同样的问题,经过几个小时的尝试,最终对我起作用的是简单地将descendantFocusability属性添加到ScrollView的包含LinearLayout的值为blocksDescendants的ScrollView中。在您的案例中:

代码语言:javascript
复制
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants" >

从那以后,这个问题就再也没有发生过。

票数 267
EN

Stack Overflow用户

发布于 2012-07-02 14:02:21

您应该创建新的类来扩展ScrollView,然后覆盖requestChildFocus:

代码语言:javascript
复制
public class MyScrollView extends ScrollView {

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

然后在您的xml布局中,使用:

代码语言:javascript
复制
<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。

票数 26
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9842494

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档