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

Nav*_*arr 105 android android-layout android-webview android-scrollview android-viewpager

所以我有一个引人入胜的问题.尽管我不是手动或以编程方式滚动我的视图,但我的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>
Run Code Online (Sandbox Code Playgroud)

我也没注重任何事情.默认情况下,它似乎在加载后自动滚动到WebView.我该如何防止这种情况?

mjp*_*p66 265

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

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:descendantFocusability="blocksDescendants" >
Run Code Online (Sandbox Code Playgroud)

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

  • android:descendantFocusability ="blocksDescendants"将是我孩子说的第一个单词. (33认同)
  • 请注意,如果存在任何子视图(如EditText),则此方法将导致它们变为不可编辑. (32认同)
  • 这对这些控件的可访问性有任何负面影响吗? (3认同)
  • 当您搜索单词ScrollView Webview autoscroll时,这个问题/答案不会出现,它应该.也许这个评论会有所帮助!使用WebViewFragment时遇到了同样的问题,即使经过大量搜索也没有找到这个问题,直到我发布了自己的问题(现已删除). (2认同)
  • 那条线:救了我的命. (2认同)

小智 41

你可以简单地将它添加到你的LinearLayout:android:focusableInTouchMode ="true".这个对我有用.

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:focusableInTouchMode="true"
    android:orientation="vertical" >
Run Code Online (Sandbox Code Playgroud)

  • 工作没有副作用! (4认同)
  • 比接受的答案更好,因为它没有副作用,而且很简单,欢呼! (3认同)

use*_*127 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);
}
}
Run Code Online (Sandbox Code Playgroud)

然后在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" >
Run Code Online (Sandbox Code Playgroud)

这对我行得通.ScrollView不再自动滚动到WebView.

  • 还需要构造函数:`public ExtendedScrollView(Context context){super(context); public InputScrollView(Context context,AttributeSet attributeSet){super(context,attributeSet); public InputScrollView(Context context,AttributeSet attributeSet,int defStyle){super(context,attributeSet,defStyle); }` (5认同)

aru*_*n-r 5

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

android:descendantFocusability="blocksDescendants"
Run Code Online (Sandbox Code Playgroud)