【问题标题】:WebView in ScrollView prevents all scrollingScrollView 中的 WebView 阻止所有滚动
【发布时间】:2013-05-08 20:05:53
【问题描述】:

我想:

  1. 在屏幕上放置一个片段,其中包含:
    1. 一个 Web 视图
    2. ...展开到全高
    3. 嵌入在上下固定大小的布局中
    4. 滚动布局(即整个内容,包括全高 web 视图)

这似乎是不可能的。我现在已经阅读了 10 多个关于 SO 的不同问题/答案,它们都涵盖了 Android WebView 中不同的严重错误,但没有一个涵盖这种情况。

似乎(通过阅读其他问题,并通过链接指向 android 网站上当前未修复的错误):

  1. WebView 的高度一直是错误的(Android 2 - 4 未修复:例如高度从不降低)
  2. WebView 的滚动中断、取消中断、重新中断在连续的 Android 版本中以新的方式(例如:某些版本 ScrollView 获得控制权,其他 WebView 获得控制权,其他他们“打架”并且您会卡顿等)
  3. 您必须对 ScrollView 进行一些奇怪的调整,以使其能够做到开箱即用。例如"android:fillViewport="true"(嗯?这不正是 "layout_height=fill_parent" 应该做的吗?)

有没有人有工作代码来实现这个相对简单和常见的设置?我会对任何可行的东西感兴趣(当然除了“扔掉你的 Android 应用程序并编写一个 web 应用程序”。这可能可行,但很遗憾,这是不切实际的)

供参考(以及其他尝试类似操作的人)这是我的布局,它填满了屏幕,但所有滚动都被禁用,我看不到任何原因:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fillViewport="true"
>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">

        <!-- page header -->

        <RelativeLayout
            android:id="@+id/fragment_detailspage_titletext_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:clickable="true"
            android:visibility="visible" >

            <include
                android:id="@+id/fragment_detailspage_titletext_include"
                layout="@layout/include_textheader"
                android:visibility="visible" />
        </RelativeLayout>

        <RelativeLayout
            android:id="@+id/fragment_detailspage_header_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/fragment_detailspage_titletext_layout"
            android:clickable="true"
            android:visibility="visible" >

            <!-- logo image -->

            <include
                android:id="@+id/fragment_detailspage_logoimageheader_include"
                layout="@layout/include_logoimageheader" />
        </RelativeLayout>

        <!-- page footer -->

        <RelativeLayout
            android:id="@+id/fragment_detailspage_footer_layout"
            android:layout_width="fill_parent"
            android:layout_height="64dp"
            android:layout_alignParentBottom="true"
            android:background="@drawable/generic_button_not_pressed"
            android:clickable="true"
            android:visibility="visible" >

            <!-- 1 buttons -->

            <include
                android:id="@+id/fragment_detailspage_bottombuttonstrip1_include"
                android:layout_width="wrap_content"
                android:layout_height="64dp"
                android:layout_centerHorizontal="true"
                layout="@layout/include_bottombuttonstrip1" />
        </RelativeLayout>

        <!-- page content -->

        <WebView
            android:id="@+id/fragment_detailspage_content_web"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_above="@id/fragment_detailspage_footer_layout"
            android:layout_below="@id/fragment_detailspage_header_layout"
            android:visibility="visible" />

    </RelativeLayout>

</ScrollView>

【问题讨论】:

  • 你看过这篇文章了吗? stackoverflow.com/a/13353874/801437
  • “我现在已经阅读了 10 多个关于 SO 的不同问题/答案,它们都涵盖了 Android WebView 中不同的严重错误,但没有一个涵盖这种情况”——我很确定我已经回答了很多 Android 问题,表明将可滚动内容放在 ScrollView 中效果不佳。
  • +1 关于 CommonsWare 的观点,不要将可滚动视图放在 ScrollView 中。您的布局看起来可能有点......过度设计。您可以将图像附加到您希望布局看起来的 Q 上,然后也许我们可以帮助您简化 xml 并解释为什么您不需要 ScrollView 中的 WebView
  • @VishalVyas 是的,我看到了那个。它使 webview 滚动,但禁用滚动视图。我正在尝试做相反的事情(从 Android 文档来看,这似乎是 INTENDED 默认值)
  • @CommonsWare - 是的,我同意它效果不佳,这是一个糟糕的课程。但它几乎被所有应用程序使用(和滥用),所以......我认为必须有一些体面的解决方法:)。也许有一个社区创作的 BetterScrollView 或类似的(我还没有找到:()等。

标签: android webview


【解决方案1】:

对不起下面的图片,但我每次都想,当有人想把可滚动视图放在可滚动视图中时。

请尝试使用 CustomScrollView 并覆盖 onInterceptTouchEvent

public class CustomScrollView extends ScrollView {

    public CustomScrollView(Context context) {
        super(context);
    }

    public CustomScrollView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomScrollView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent e) {
        super.onInterceptTouchEvent(e);
        return false;
    }


}

【讨论】:

    【解决方案2】:

    我使用的几乎所有应用都有某种形式的网页/滚动/自定义页眉/页脚组合

    记住“Web”可以是TextView 而不是WebView,记住“滚动”可以是ListView 而不是ScrollView,记住“自定义页眉/页脚组合" 可以在WebView 中的 HTML 中实现。其中任何一个都可以让您摆脱WebView-in-a-ScrollView 的情况。很少有应用程序会使用ScrollView,而且会在ScrollView 中尝试WebView 的应用程序更少。

    当然还有更复杂的可能性。例如,Gmail 的对话视图是一个WebView,其他东西在 Z 轴上使用FrameLayout 分层在它的顶部,他们大概跟踪WebView 的滚动事件以更改其他分层顶部的东西的位置其中(例如,当您滚动到顶部时固定发送者和回复按钮标题)。

    如果您拥有 Android 4.1+ 设备,欢迎您使用 uiautomatorviewer 了解如何实现此类内容。这就是我看到 Gmail 是如何成功的。你无法获得所有细节,但它会为你提供线索。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-06
      • 1970-01-01
      • 2012-10-26
      • 1970-01-01
      相关资源
      最近更新 更多