在Android 4.x上,ViewPager内部的ScrollView无法滚动

5

我有一个问题,似乎只影响 Android 4.x 版本,可能也是特定设备的问题 (即在我的华为 G630@4.3 上不存在,但在三星 Ace2@4.4.4 上存在)。 我有一个包含 4 个 CardView 的 RelativeLayout 的 ScrollView。现在,在一些 4.x 设备上,当我尝试从卡片开始滚动时,滚动事件根本不会发生。如果我触摸卡片之间或第一个卡片上方 (而不是任意两个卡片之间) 和设备屏幕之间的小间隔,我就可以滚动内容。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                    xmlns:app="http://schemas.android.com/tools"
                    xmlns:card_view="http://schemas.android.com/apk/res-auto"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="@dimen/activity_horizontal_margin">

        <android.support.v7.widget.CardView
            android:id="@+id/metricsContainerCard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            app:cardElevation="2dp"
            card_view:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="10dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="5dp"
                        android:src="@drawable/metrics"/>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/metrics"
                        android:textSize="20sp"/>
                </LinearLayout>


                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/projectCircleBackgroundShadow"/>

            </LinearLayout>

        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/warningsContainerCard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_below="@id/metricsContainerCard"
            app:cardElevation="2dp"
            card_view:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="10dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="5dp"
                        android:src="@drawable/bug_color"/>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/warnings"
                        android:textSize="20sp"/>
                </LinearLayout>


                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/projectCircleBackgroundShadow"/>

            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/topWarningsContainerCard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/warningsContainerCard"
            android:layout_marginTop="10dp"
            app:cardElevation="2dp"
            card_view:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="10dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="5dp"
                        android:src="@drawable/top_warnings"/>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/topWarnings"
                        android:textSize="20sp"/>
                </LinearLayout>


                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/projectCircleBackgroundShadow"/>

            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/topCriticalItemsContainerCard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/topWarningsContainerCard"
            android:layout_marginTop="10dp"
            app:cardElevation="2dp"
            card_view:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:padding="10dp">

                <LinearLayout
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:layout_width="20dp"
                        android:layout_height="20dp"
                        android:layout_gravity="center_vertical"
                        android:layout_marginRight="5dp"
                        android:src="@drawable/top_critical_items"/>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:text="@string/topCriticalItems"
                        android:textSize="20sp"/>

                </LinearLayout>


                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/projectCircleBackgroundShadow"/>

            </LinearLayout>
        </android.support.v7.widget.CardView>
    </LinearLayout>
</ScrollView>

在Android 5.x和6.x上没有任何问题。

编辑

似乎问题并不专门与CardView有关,因为我有另一个布局,即使我通过自定义视图拖动屏幕,它也无法滚动:

<ScrollView
    android:id="@+id/scrollView"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context=".projects.details.ProjectDetailsActivity_">

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  xmlns:tools="http://schemas.android.com/tools"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content"
                  android:orientation="vertical"
                  android:padding="@dimen/activity_horizontal_margin">


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

            <TextView
                android:id="@+id/projectName"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="16dp"
                android:ellipsize="none"
                android:gravity="center_horizontal"
                android:text="Project name"
                android:textSize="24sp"/>

            <c.f.q.a.projects.details.components.widgets.ProjectDetailWidget
                android:id="@+id/projectDetailWidget"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="center"/>
        </LinearLayout>

        <LinearLayout
            android:id="@+id/timelineWrapperLayout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <android.support.v7.widget.CardView
                android:id="@+id/qualityTimelineCard"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                app:cardElevation="2dp"
                card_view:cardUseCompatPadding="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="10dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:id="@+id/flipQualityTimelineButton"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_marginRight="5dp"
                            android:src="@drawable/flip_to_cost"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:ellipsize="end"
                            android:lines="1"
                            android:text="@string/project_timeline_title"
                            android:textSize="16dp"/>
                    </LinearLayout>

                    <ProgressBar
                        android:id="@+id/projectQualityTimelineProgressBar"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        />

                    <com.github.mikephil.charting.charts.LineChart
                        android:id="@+id/projectQualityTimeline"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:layout_marginTop="16dp"
                        android:tag="@string/project_chart_tag"
                        android:visibility="gone"/>
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/costTimelineCard"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:visibility="gone"
                app:cardElevation="2dp"
                card_view:cardUseCompatPadding="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    android:padding="10dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:id="@+id/flipCostTimelineButton"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_marginRight="5dp"
                            android:src="@drawable/flip_to_quality"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:ellipsize="end"
                            android:lines="1"
                            android:text="@string/project_cost_timeline_title"
                            android:textSize="16dp"/>
                    </LinearLayout>


                    <ProgressBar
                        android:id="@+id/projectCostTimelineProgressBar"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        />

                    <com.github.mikephil.charting.charts.LineChart
                        android:id="@+id/projectCostTimeline"
                        android:layout_width="match_parent"
                        android:layout_height="200dp"
                        android:layout_marginTop="16dp"
                        android:tag="@string/project_chart_tag"
                        android:visibility="gone"/>
                </LinearLayout>
            </android.support.v7.widget.CardView>

        </LinearLayout>

        <android.support.v7.widget.CardView
            android:id="@+id/vcsChangeCard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            app:cardElevation="2dp"
            card_view:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:orientation="vertical"
                android:padding="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:id="@+id/expandVcsChangeButton"
                        android:layout_width="28dp"
                        android:layout_height="28dp"
                        android:layout_marginRight="5dp"
                        android:src="@drawable/expand_chart"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:ellipsize="end"
                        android:maxLines="2"
                        android:text="@string/project_vcschange_title"
                        android:textSize="16sp"/>
                </LinearLayout>

                <ProgressBar
                    android:id="@+id/vcsChangeProgressBar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"/>

                <com.github.mikephil.charting.charts.BarChart
                    android:id="@+id/projectVcsChange"
                    android:layout_width="match_parent"
                    android:layout_height="175dp"
                    android:layout_marginTop="16dp"
                    android:tag="@string/project_chart_tag"
                    android:visibility="gone"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <android.support.v7.widget.CardView
            android:id="@+id/dtnosChangeCard"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1"
            app:cardElevation="2dp"
            card_view:cardUseCompatPadding="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="right"
                android:orientation="vertical"
                android:padding="10dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content">

                    <ImageView
                        android:id="@+id/expandDtnosChangeButton"
                        android:layout_width="28dp"
                        android:layout_height="28dp"
                        android:layout_marginRight="5dp"
                        android:src="@drawable/expand_chart"/>

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_vertical"
                        android:ellipsize="end"
                        android:maxLines="2"
                        android:text="@string/project_systemchange_title"
                        android:textSize="16sp"/>
                </LinearLayout>

                <ProgressBar
                    android:id="@+id/dtnosChangeProgressBar"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"/>

                <com.github.mikephil.charting.charts.BarChart
                    android:id="@+id/projectDtnosChange"
                    android:layout_width="match_parent"
                    android:layout_height="175dp"
                    android:layout_marginTop="16dp"
                    android:tag="@string/project_chart_tag"
                    android:visibility="gone"/>
            </LinearLayout>
        </android.support.v7.widget.CardView>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <android.support.v7.widget.CardView
                android:id="@+id/sensorNodeCard"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                app:cardElevation="2dp"
                card_view:cardUseCompatPadding="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="left"
                    android:orientation="vertical"
                    android:padding="10dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:id="@+id/expandSensornodeButton"
                            android:layout_width="28dp"
                            android:layout_height="28dp"
                            android:layout_marginRight="5dp"
                            android:src="@drawable/expand_chart"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:ellipsize="end"
                            android:maxLines="2"
                            android:text="@string/project_sensornode_title"
                            android:textSize="16sp"/>
                    </LinearLayout>


                    <ProgressBar
                        android:id="@+id/sensorNodeProgressBar"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"/>

                    <com.github.mikephil.charting.charts.RadarChart
                        android:id="@+id/sensorNodeChart"
                        android:layout_width="match_parent"
                        android:layout_height="175dp"
                        android:layout_marginTop="16dp"
                        android:tag="@string/project_chart_tag"
                        android:visibility="gone"/>
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <android.support.v7.widget.CardView
                android:id="@+id/aggregateNodeCard"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dp"
                android:layout_weight="1"
                app:cardElevation="2dp"
                card_view:cardUseCompatPadding="true">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:gravity="right"
                    android:orientation="vertical"
                    android:padding="10dp">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:id="@+id/expandAggregateButton"
                            android:layout_width="28dp"
                            android:layout_height="28dp"
                            android:layout_marginRight="5dp"
                            android:src="@drawable/expand_chart"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_gravity="center_vertical"
                            android:ellipsize="end"
                            android:maxLines="2"
                            android:text="@string/project_aggregatenode_title"
                            android:textSize="16sp"/>

                    </LinearLayout>

                    <ProgressBar
                        android:id="@+id/aggregateNodeProgressBar"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"/>

                    <com.github.mikephil.charting.charts.RadarChart
                        android:id="@+id/aggregateNodeChart"
                        android:layout_width="match_parent"
                        android:layout_height="175dp"
                        android:layout_marginTop="16dp"
                        android:tag="@string/project_chart_tag"
                        android:visibility="gone"/>
                </LinearLayout>
            </android.support.v7.widget.CardView>
        </LinearLayout>
    </LinearLayout>
</ScrollView>

我添加了完整的布局,并提供了一张图片以便澄清(我标记了可滚动区域,可以拖动):scrollable areas 编辑2 也许重要的是这些布局是 ViewPager 中的片段。是否有可能 ViewPager 防止 ScrollView (以及其中的任何其他视图)获得焦点/处理触摸事件?我的 ViewPager 代码如下:
<android.support.v4.view.ViewPager
    android:id="@+id/projectPager"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

EDIT3

看起来问题肯定是由于ViewPager + (Nested)ScrollView引起的(我试图用纯的ScrollView替换它,但没有成功)。我为我的片段创建了一个虚拟布局,只包含一个父级NestedScrollView、一个LinearLayout和很多大的TextView,如下所示:

<android.support.v4.widget.NestedScrollView
android:id="@+id/scrollView"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".projects.details.ProjectDetailsActivity_">

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              xmlns:tools="http://schemas.android.com/tools"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:orientation="vertical"
              android:padding="@dimen/activity_horizontal_margin">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="16dp"
        android:ellipsize="none"
        android:gravity="center_horizontal"
        android:text="Project name"
        android:textSize="80sp"/>

... <!-- more TextViews come here -->

...而且滚动也不起作用!只有在屏幕的边缘才能滚动,就像以前一样。我不知道这是怎么发生的...


从您的编辑中看来,它与 CardView 无关 - 或许您可以重新编辑以省略 CardView,并提供关于其余设置的更多信息。 - ataulm
你是否尝试设置CardViewandroid:clickable="true"android:focusable="true"属性? - fillobotto
@Sleeper9 好的,看到我编辑过的答案了。 - DJafari
1
尝试过了,还是没有成功。我想问一下,这是否可能是因为我的CardViews稍后会被“填充”(例如添加更多的View)所致?我做了一个非常小的样例,只包含TextViewImageViewScrollView,我可以滚动内容。 - Sleeper9
目前还没有解决这个问题。由于这个问题只出现在相当老旧的设备上,所以我放弃了调试。你有同样的问题吗? - Sleeper9
显示剩余3条评论
4个回答

2
您说得对,两个可滚动控件之间存在竞争关系。
请使用来自支持库的NestedScrollView代替。它是为了解决这种类型的问题而设计的。
它有许多增强功能,可以让它与其他可滚动控件一起使用,包括ViewPager、RecyclerView(只要调用.setNestedScrolling(true))和CoordinatorLayout。
基于以上内容,我已经在所有实例中使用了这个新类,并且滚动内部滚动已经完美地工作。我们的产品已经上线,其中嵌入了RecyclerView列表的NestedScrollView控件也能够完美地工作。

1
我已经将<ScrollView>替换为<android.support.v4.widget.NestedScrollView>,但仍然没有任何改变。:( 我也调用了.setNestedScrolling(true)。在XML布局中是否有需要设置的内容? - Sleeper9
好的,很抱歉这次没能提供帮助。我会将这个留在这里,以备其他读者需要。这是社区维基,任何人都可以编辑和改进。 - Richard Le Mesurier
1
但问题肯定出在这里……就像我所写的那样,我的滚动视图中的其他视图也没有响应触摸、缩放等事件,所以ViewPager肯定会在某种程度上消耗手势… - Sleeper9

2

问题已解决!我遇到了完全相同的问题。您可能正在使用PageTransformer ViewPager动画。

禁用自定义页面转换器,适用于< Android 4.1即可解决问题:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
        // there are problems with this on 4.0.3, probably also on 4.1
        viewPager.setPageTransformer(true, new DepthPageTransformer());
    }

您的帖子和研究为我节省了很多时间,谢谢。

其他解决方案:

如果您喜欢动画效果,您也可以尝试将DepthPageTransformer更改为此:https://stackoverflow.com/a/28214802/1310343c


我简直不敢相信... 非常感谢,这也解决了我的问题! :) - Sleeper9

2
对于我来说,这种设置是有效的:我有一个Coordinator布局,其中包含下面的嵌套滚动视图。
 <android.support.v4.widget.NestedScrollView
    android:layout_marginTop="8dp"
    android:fillViewport = "true"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

  <include layout="@layout/content_app_bar_search_patient_module"/>

</android.support.v4.widget.NestedScrollView>

<include layout="@layout/content_app_bar_search_patient_module"/>包含了一个视图页,其中包含了一个再次嵌套滚动视图的片段。

<android.support.v4.widget.NestedScrollView 

xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
.
.
.
    </android.support.v4.widget.NestedScrollView>

-1

你的 ScrollView 必须具有 match_parent height,并且滚动视图的第一个子元素(LinearLayout)必须具有 wrap_content 高度。


我之前已经尝试过了(我还不小心在我的示例中留下了一个 android:orientation="vertical"),但并没有帮助。我目前正在尝试使用 Genymotion Google Nexus S@4.1.1 - Sleeper9
@Sleeper9请完整提供您的XML布局。 - DJafari
我已经编辑了我的帖子,添加了完整的布局和一张图片以便澄清! - Sleeper9

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接