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

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

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

我在浏览器中有一个 fragment 。当我第一次加载寻呼机时,它按预期工作并显示了所有内容。但是,一旦我“翻转页面”,数据就会加载,WebView 会弹出到页面顶部,隐藏其上方的 View ,这是不可取的。

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

我的布局是这样的:

<?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。如何防止这种情况发生?

最佳答案

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

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

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

关于android - 加载数据后如何防止 ScrollView 滚动到 WebView ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9842494/

相关文章:

android - 如何从单独的类中动态更改 ListView?

android-studio - 在webview android studio中设置cookie

FragmentActivity 中的 Android admob

android - 运行测试时出现问题(<androidx.test.ext.junit.runners.AndroidJUnit4> 没有零参数构造函数)

php - Apple Sign In "invalid_client",使用 PHP 和 openSSL 签署 JWT 进行身份验证

android - 布局上的 "tag"和 "id"是什么?

java - 如何确定选项卡中 fragment 的 XML 布局?

Android Webview : E/chromium: [ERROR: ssl_client_socket_impl. cc (1141)]

android - 如何禁用 webview 的垂直滚动以将整个页面加载为不同的列

android - Flutter Kotlin从广播接收器获取数据