【问题标题】: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_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。如何防止这种情况发生?

【问题讨论】:

  • 为什么需要 ScrollView,因为 WebView 是可滚动的?
  • 将某些方面保留在 webview 之外,而不是将它们呈现为 HTML。为了速度和质量。
  • mjp66 有最好的答案 - 也许你应该接受他的答案

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


【解决方案1】:

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

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

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

【讨论】:

  • 这对这些控件的可访问性有负面影响吗?
  • 注意,如果有像EditText这样的子视图,这个方法会导致它们变得不可编辑。
  • 当您搜索 ScrollView Webview autoscroll 时不会出现此问题/答案,它应该出现。也许这条评论会有所帮助!使用 WebViewFragment 时遇到同样的问题,即使经过大量搜索也没有找到这个问题,直到我发布了自己的问题(现已删除)。
  • 在 Xamarin.Android 项目上工作,在具有多个网格视图的滚动视图中出现自动滚动问题,并与下载的图像异步填充。这个解决方案非常完美,我乍一看都不敢相信,不得不再次测试。
  • RecyclerView 中的广告也会出现同样的问题。您的解决方案也适用于这种情况:)
【解决方案2】:

您可以简单地将其添加到您的 LinearLayout:android:focusableInTouchMode="true"。它对我有用。

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    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_width="fill_parent"
    android:layout_height="fill_parent"
    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_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" >
    

    【讨论】:

      【解决方案6】:

      可能有人和我有同样的问题,所以我会帮忙。

      我试图将 android:descendantFocusability="beforeDescendants" 放在我的主 ScrollView 中,如下所示:

      <ScrollView
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          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_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 */
      

      【讨论】:

        【解决方案7】:

        我必须为 MyScrollView 使用完全限定名称,否则我会得到一个 inflate 异常。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2019-11-27
          • 1970-01-01
          • 2013-07-23
          相关资源
          最近更新 更多