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

Android 如何防止数据加载后滚动视图滚动到webview?,android,android-layout,android-webview,android-viewpager,android-scrollview,Android,Android Layout,Android Webview,Android Viewpager,Android Scrollview,所以我有一个有趣的问题。尽管我没有手动或编程滚动我的视图,但我的WebView在加载其中的数据后会自动滚动到 我在寻呼机里找到了一个片段。当我第一次加载寻呼机时,它按预期工作,所有内容都显示出来。但一旦我“翻转页面”,数据就会加载,网络视图就会弹出到页面顶部,隐藏页面上方的视图,这是不可取的 有人知道如何防止这种情况发生吗 我的布局如下所示: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="ht

所以我有一个有趣的问题。尽管我没有手动或编程滚动我的视图,但我的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。如何防止这种情况发生?

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

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

我也遇到了同样的问题,在尝试了几个小时后,最终对我有效的方法是向ScrollView的包含LinearLayout的属性中添加
DegenantFocusability
属性,该属性的值为
BlocksDescents
。就你而言:

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


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

您只需将其添加到线性布局中即可:android:focusableInTouchMode=“true”。它对我有用

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

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

android:descendantFocusability="blocksDescendants"
像这样:

<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" >

可能有人和我有同样的问题,所以我会帮助他们

我试图在我的主滚动视图中放置android:DegenantFocusability=“BeforeMedians”,如下所示:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants">
    /*my linearlayout or whatever to hold the views */
<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 */

/*我的直线布局或其他什么来保持观点*/
而且它不起作用,所以我必须在ScrollView的父对象上创建一个相对关系,并将安卓:genderantfocusability=“beforesands”也放在父对象中

所以我通过以下方法解决了这个问题:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="beforeDescendants">
    /*my linearlayout or whatever to hold the views */
<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 */

/*我的直线布局或其他什么来保持观点*/

为什么您需要ScrollView,因为WebView是可滚动的?将某些方面保留在WebView之外,而不是将其呈现为HTML。对于速度和质量,mjp66有最好的答案——也许你应该接受他的答案。还需要构造函数:
public ExtendedScrollView(Context-Context){super(Context);}public ExtendedScrollView(Context-Context,AttributeSet-AttributeSet){super(Context,AttributeSet);}public ExtendedScrollView(上下文上下文,属性集,属性集,int-defStyle){super(上下文,属性集,defStyle);}
这会对这些控件的可访问性产生任何负面影响吗?请注意,如果存在EditText之类的子视图,此方法将导致它们不可编辑。当您搜索ScrollView Webview autoscroll(滚动视图Webview自动滚动)一词时,不会出现此问题/答案,它应该会出现。也许此注释会有所帮助!与此问题相同的是w我正在使用WebViewFragment,在我发布我自己的问题(现在已删除)之前,即使经过大量搜索也没有找到此问题。在Xamarin.Android项目中工作时,在具有多个GridView的scrollview中出现自动滚动问题,异步填充了下载的图像。此解决方案工作得非常完美,我第一眼就难以相信,不得不再次测试。同样的问题也发生在
RecyclerView
中的广告上。您的解决方案工作正常在这种情况下:)没有副作用!