加载数据后如何防止滚动视图滚动到网络视图?
Posted
技术标签:
【中文标题】加载数据后如何防止滚动视图滚动到网络视图?【英文标题】:How to prevent a scrollview from scrolling to a webview after data is loaded? 【发布时间】:2012-04-08 04:58:47 【问题描述】:所以我有一个有趣的问题。尽管我没有手动或以编程方式滚动我的视图,但我的 WebView 会在其中的数据加载后自动滚动到。
我在浏览器中有一个片段。当我第一次加载寻呼机时,它按预期工作并显示了所有内容。但是,一旦我“翻转页面”,数据就会加载,WebView 会弹出到页面顶部,隐藏其上方的视图,这是不可取的。
有谁知道如何防止这种情况发生?
我的布局是这样的:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="@color/background" >
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical" >
<TextView
android:id="@+id/article_title"
android:layout_
android:layout_
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_
android:layout_
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_
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_ />
<TextView
android:id="@+id/article_link"
android:layout_
android:layout_
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。如何防止这种情况发生?
【问题讨论】:
为什么需要 ScrollView,因为 WebView 是可滚动的? 将某些方面保留在 webview 之外,而不是将它们呈现为 html。为了速度和质量。 mjp66 有最好的答案 - 也许你应该接受他的答案 【参考方案1】:我遇到了同样的问题,在尝试了几个小时后,最终对我有用的只是将descendantFocusability
属性添加到包含LinearLayout 的ScrollView,值为blocksDescendants
。在你的情况下:
<LinearLayout
android:layout_
android:layout_
android:orientation="vertical"
android:descendantFocusability="blocksDescendants" >
从那以后问题再没有出现过。
【讨论】:
这对这些控件的可访问性有负面影响吗? 注意,如果有像EditText这样的子视图,这个方法会导致它们变得不可编辑。 当您搜索 ScrollView Webview autoscroll 时不会出现此问题/答案,它应该出现。也许这条评论会有所帮助!使用 WebViewFragment 时遇到同样的问题,即使经过大量搜索也没有找到这个问题,直到我发布了自己的问题(现已删除)。 在 Xamarin.Android 项目上工作,在具有多个网格视图的滚动视图中出现自动滚动问题,并与下载的图像异步填充。这个解决方案非常完美,我乍一看都不敢相信,不得不再次测试。RecyclerView
中的广告也会出现同样的问题。您的解决方案也适用于这种情况:)【参考方案2】:
您可以简单地将其添加到您的 LinearLayout:android:focusableInTouchMode="true"。它对我有用。
<LinearLayout
android:layout_
android:layout_
android:focusableInTouchMode="true"
android:orientation="vertical" >
【讨论】:
无副作用!【参考方案3】:您应该创建新类扩展 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_
android:layout_
android:background="@color/background" >
这对我有用。 ScrollView 将不再自动滚动到 WebView。
【讨论】:
还需要构造函数:public ExtendedScrollView(Context context) super(context); public ExtendedScrollView( Context context, AttributeSet attributeSet) super(context, attributeSet); public ExtendedScrollView( Context context, AttributeSet attributeSet, int defStyle) super(context, attributeSet, defStyle);
【参考方案4】:
在主布局中添加这些行可以解决问题
android:descendantFocusability="blocksDescendants"
【讨论】:
【参考方案5】:像这样:
<com.ya.test.view.MyScrollView
android:layout_
android:layout_ >
<FrameLayout
android:layout_
android:layout_
android:descendantFocusability="beforeDescendants"
android:focusable="true"
android:focusableInTouchMode="true" >
【讨论】:
【参考方案6】:可能有人和我有同样的问题,所以我会帮忙。
我试图将 android:descendantFocusability="beforeDescendants" 放在我的主 ScrollView 中,如下所示:
<ScrollView
android:layout_
android:layout_
android:descendantFocusability="beforeDescendants">
/*my linearlayout or whatever to hold the views */
它不起作用,所以我不得不将一个 RelativeLayout 作为 ScrollView 的父级,并将 android:descendantFocusability="beforeDescendants" 也放在父级中。
所以我通过以下方式解决了它:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:descendantFocusability="blocksDescendants">
<ScrollView
android:layout_
android:layout_>
/*my linearlayout or whatever to hold the views */
【讨论】:
【参考方案7】:我必须为 MyScrollView 使用完全限定名称,否则我会得到一个 inflate 异常。
<com.mypackagename.MyScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:background="@color/background" >
【讨论】:
以上是关于加载数据后如何防止滚动视图滚动到网络视图?的主要内容,如果未能解决你的问题,请参考以下文章
水平 UIScrollView 内部有垂直 UIScrollViews - 滚动外部水平视图时如何防止滚动内部滚动视图?