【问题标题】:Clickable CardView inside NestedScroll doesn't trigger scrolling嵌套滚动中的可点击 CardView 不会触发滚动
【发布时间】:2015-07-09 14:17:28
【问题描述】:

我有一个带有 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>

【问题讨论】:

  • 开放赏金+50有点可惜,但这绝对是stackoverflow.com/questions/31136740/…的重复 - 删除赏金并标记重复。
  • 情况不一样,虽然从标题看来确实如此,但我认为这里真正的问题是,当我将卡片视图设置为可点击时,触摸事件不会冒泡到滚动视图。我在滚动视图中确实有 fill_vertical 布局,这为我解决了一些问题,但它并没有修复带有可点击元素的滚动
  • 也许你是对的,但可能我们需要在拦截触摸事件上重写以不传播到返回 true 的子视图。
  • 我应该在哪个视图中处理拦截触摸事件?
  • 我给你的另一页中答案的所有者评论说他确实没有解决问题,并且 "看起来问题出在 CollapsingToolbar 上,因为如果我们删除 CollapsingToolbar它开始工作正常”。现在我们有两个选择:等待所有这些多个错误都被 Google 解决,或者尝试实现一些东西。对于最后一个假设,我认为覆盖 onInterceptTouchEvent 的方法,您可以从此类 FixedScrollingViewBehavior 开始,您可以在这里找到已经解决了一些问题的 gist.github.com/EmmanuelVinas/c598292f43713c75d18e

标签: android android-design-library android-collapsingtoolbarlayout android-coordinatorlayout android-appbarlayout


【解决方案1】:

当 ScrollView 没有填充内容时,问题就来了。我认为这是Android中一个非常奇怪的错误。

“解决方案”..

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

【讨论】:

  • 这修复了滚动但上部元素滚动超出窗口的问题。
  • 是的。我想问题是关于滚动问题。如果其他视图出现在窗口之外,听起来你的配置中还有其他一些错误。
  • 我已尝试进行适当的修复,但没有成功。谷歌的新课程太复杂了。改用 RecyclerView 会更容易。
  • 在我看来,今天(!)整个库都是垃圾。我在错误查找/修复上浪费了很多时间,最后我没有得到正确的结果。 :-/ 也许是因为我不是专业人士 - 但是在更新到 22.2.1 之后, NavigationView 的 setIcon(new Drawable) 不再工作了。 Locgat也在发射废话……非常非常奇怪。也许我们必须等待 22.2.x(?)...
  • 到23.0.1这个bug依然存在!!并且没有很难修复的源代码。
【解决方案2】:

这似乎和这里的问题一样:Scroll doesn't work in NestedScrollView when try to scroll from views with click events

这是解决您的问题的一种解决方法 https://stackoverflow.com/a/32887429/2165810

但您也应该使用 setPreventCornerOverlap(false) 作为您的 CardView 以避免 CardView 出现问题

【讨论】:

    【解决方案3】:

    正如人们所指出的,问题是当CardView 完全位于屏幕内时,它不会触发从卡片滚动。在CollapsingToolbarLayout 内的视图中滚动仍然有效。

    我的解决方案是将android:layout_marginBottom="100dp" 添加到CardView,这样底部边缘就在屏幕外,从而允许滚动。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-06-23
      • 1970-01-01
      • 2014-10-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多