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

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

37112 次浏览

You should create new class extend ScrollView, then Override requestChildFocus:

public class MyScrollView extends ScrollView {


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

Then in your xml layout, using:

<MyScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@color/background" >

That works for me. The ScrollView will not auto scroll to the WebView anymore.

I had to use the fully qualified name for MyScrollView, otherwise I got an inflate exception.

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

I had the same problem, after hours of trying several ideas, what finally worked for me was simply adding the descendantFocusability attribute to the ScrollView's containing LinearLayout, with the value blocksDescendants. In your case:

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

Haven't had the problem reoccur since.

You can simply add this to your LinearLayout: android:focusableInTouchMode="true". It works for me.

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

Adding these line in main layout solves the problem

android:descendantFocusability="blocksDescendants"

Like this:

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

Probably there are people who have the same problem I was having, so I'll help out.

I was trying to put android:descendantFocusability="beforeDescendants" in my main ScrollView as following:

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants">
/*my linearlayout or whatever to hold the views */

and it wasn't working, so I had to make a RelativeLayout the parent of the ScrollView, and place the android:descendantFocusability="beforeDescendants" in the parent aswell.

So I solved it doing the following:

<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 */

I added: android:overScrollMode="never" in my ScrollView and set the height to wrap_content.

My view was very complex as it was legacy code with LinearLayout inside LinearLayout inside LinearLayout.

This helped me, hope it will help someone else too!