可点击的CardView在NestedScroll中无法触发滚动。

19

我有一个包含CoordinatorLayout、AppBarLayout和NestedScrollView的布局,在NestedScrollView中,我有多个CardView。一切都工作得很好,直到我将CardView设置为可点击,此时,如果我在CardView内开始滚动,滚动就不起作用了。

这是我的布局:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
    android:id="@+id/appbar"
    android:layout_height="256dp"
    android:layout_width="match_parent"
    app:contentScrim="?attr/colorPrimary"
    android:fitsSystemWindows="true"
    app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary">
        <ImageView
            android:id="@+id/backdrop"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scaleType="centerCrop"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"/>
        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_height="?attr/actionBarSize"
            android:layout_width="match_parent"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />
    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
    android:id="@+id/nestedScroll"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_gravity="fill_vertical">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">
        <android.support.v7.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:cardCornerRadius="0dp"
            app:cardUseCompatPadding="true">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
                <View
                    android:layout_width="80dp"
                    android:layout_height="80dp"
                    android:background="@color/primary" />
                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:paddingTop="4dp"
                    android:paddingLeft="7dp"
                    android:paddingRight="7dp"
                    android:paddingBottom="7dp">
                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:lines="1"
                        android:includeFontPadding="false"
                        android:text="Title1"
                        style="@android:style/TextAppearance.Medium" />
                    <TextView
                        android:lines="2"
                        android:text="Description 1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content" />
                </LinearLayout>
            </LinearLayout>
        </android.support.v7.widget.CardView>
       ...
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>


3
很遗憾这个开放赏金问题+50,但这明显是一个重复的问题,与https://dev59.com/M-o6XIcBkEYKwwoYFQHR相同 - 撤销赏金并标记为重复。 - Davideas
2
虽然从标题看起来似乎是相同的情况,但实际上并非如此。我认为这里的真正问题是当我将卡片视图设置为可点击时,触摸事件没有冒泡到滚动视图。 我在滚动视图中有fill_vertical布局,并且这解决了我的一些问题,但它无法修复包含可点击元素的滚动。 - Escobar5
1
也许你是对的,但是我们可能需要重写拦截触摸事件以防止其传递到子视图并返回 true。 - Davideas
4
我收到的另一页答案的所有者评论说,他确实没有解决这个问题,并且“看起来问题出在CollapsingToolbar上,因为如果我们移除CollapsingToolbar它就开始正常工作了”。现在我们有两个选项:等待Google解决所有这些多个错误,或者尝试实现一些东西。对于这个最后的假设,我认为可以重写onInterceptTouchEvent方法,并且您可以从这里找到FixedScrollingViewBehavior类开始 https://gist.github.com/EmmanuelVinas/c598292f43713c75d18e 它已经解决了一些问题。 - Davideas
这个问题已经在支持库的23.1.0版本中得到了修复。 - Abhishek V
显示剩余6条评论
3个回答

7
问题在于ScrollView没有填满内容时会显露出来。我认为这是Android中非常奇怪的一个bug。
“解决方案”... enter image description here
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".MainActivity">

    <android.support.design.widget.AppBarLayout
        android:id="@+id/appbar"
        android:layout_height="256dp"
        android:layout_width="match_parent"
        app:contentScrim="?attr/colorPrimary"
        android:fitsSystemWindows="true"
        app:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary">

            <ImageView
                android:id="@+id/backdrop"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:scaleType="centerCrop"
                android:fitsSystemWindows="true"
                app:layout_collapseMode="parallax"/>

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_height="?attr/actionBarSize"
                android:layout_width="match_parent"
                app:layout_collapseMode="pin"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light"/>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v4.widget.NestedScrollView
        android:id="@+id/nestedScroll"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:layout_gravity="fill_vertical">

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

            <android.support.v7.widget.CardView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:cardCornerRadius="0dp"
                app:cardUseCompatPadding="true"
                android:foreground="?android:attr/selectableItemBackground"
                android:clickable="true">

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

                    <View
                        android:layout_width="80dp"
                        android:layout_height="80dp"
                        android:background="#cdcdcd"
                        android:clickable="true"/>

                    <LinearLayout
                        android:orientation="vertical"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="4dp"
                        android:paddingLeft="7dp"
                        android:paddingRight="7dp"
                        android:paddingBottom="7dp">

                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:lines="1"
                            android:includeFontPadding="false"
                            android:text="Title1"
                            style="@android:style/TextAppearance.Medium"/>

                        <TextView
                            android:lines="2"
                            android:text="Description 1"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            />
                    </LinearLayout>
                </LinearLayout>
            </android.support.v7.widget.CardView>

            <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="1000dp"
            android:orientation="vertical"
            android:layout_margin="4dp"
            android:background="#c1c1c1">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20pt"
                android:text="some content..."/>
        </LinearLayout>

        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>

这修复了滚动,但是上部分素材仍然可以滚出窗口。 - Fedor Kazakov
是的,我想问题是关于滚动问题的。如果其他视图出现在窗口之外,那么这似乎意味着您的配置中存在一些其他错误。 - Martin Pfeffer
1
我尝试过进行适当的修复,但是无法成功。谷歌的新类太复杂了。使用RecyclerView更容易些。 - Fedor Kazakov
1
在我看来,整个库(指某一编程库)今天(即目前)都是垃圾。我浪费了很多时间去寻找/修复错误,最后也没有得到正确的结果。:-/ 也许这是因为我不是专业人士——但是在更新为22.2.1版本后,NavigationView的setIcon(new Drawable)不再起作用。Locgat也输出了无用的信息,非常奇怪。也许我们需要等待22.2.x版本... - Martin Pfeffer
2
到23.0.1版本为止,这个bug仍然存在!!而且没有源代码,非常难以修复。 - DouO
显示剩余2条评论

0
正如人们所指出的,问题在于当CardView完全位于屏幕内时,它不会从卡片触发滚动。仍然可以从CollapsingToolbarLayout中的视图进行滚动。
我的解决方案是将android:layout_marginBottom="100dp"添加到CardView中,以使底部边缘脱离屏幕,从而允许滚动。

0

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