19

I have a layout with a CoordinatorLayout, and AppBarLayout and a NestedScrollView, inside the NestedScrollView I have multiple CardViews, everything works ok until I set the CardViews to be clickable, then if I start a scroll within a CardView, scroll doesn't work.

This is my layout:

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

Escobar5
  • 3,941
  • 8
  • 39
  • 62
  • 3
    It's a pity for the open bounty +50, but this is definitively a duplicate of http://stackoverflow.com/questions/31136740/scroll-doesnt-work-in-nestedscrollview-when-try-to-scroll-from-views-with-click - Remove bounty and mark duplicate. – Davideas Jul 13 '15 at 15:37
  • 2
    It´s not the same case, although from the title it does seem like it is, I think the real problem here is that when I set the cardviews as clickable, then the touch event doesn´t bubble up to the scrollview. I do have the fill_vertical layout in the scrollview, and that fixed some issues for me, but it doesn´t fix the scroll with the clickable elements – Escobar5 Jul 13 '15 at 15:49
  • 1
    Maybe you are right, but probably we need to override on intercept touch event to no be propagated to the child view returning true. – Davideas Jul 13 '15 at 22:59
  • In which view whould I handle the intercept touch event? – Escobar5 Jul 14 '15 at 14:02
  • 4
    The owner of the answer in the other page I gave you commented that indeed he didn't resolve the problem and that *"it looks like the problem is with CollapsingToolbar, because if we remove CollapsingToolbar it started working fine"*. Now we have 2 options: wait that all these multiple bugs are resolved by Google, or try to implement something. For this last hypothesis the method to override I think onInterceptTouchEvent and you can start from this class `FixedScrollingViewBehavior` you can find here https://gist.github.com/EmmanuelVinas/c598292f43713c75d18e that already resolve some problems. – Davideas Jul 14 '15 at 16:00
  • Not working for me. Very annoying... But anyway, thanks :) – Martin Pfeffer Jul 29 '15 at 23:44
  • Did you this issue solve??? – goldsmit409 Jul 30 '15 at 17:58
  • Right now, I think I spotted the issue... – Martin Pfeffer Jul 30 '15 at 19:39
  • Take a look at this demo by Chris Banes https://github.com/chrisbanes/cheesesquare It has a CardViews inside NestedScrollView, and if you make them clickable it'll work just fine. – Pavlo Zin Aug 01 '15 at 13:50
  • @PaulZin Yes, that demo works with click event, but it seems that it's working because of the height of the content, if I take away part of the string "cheese_ipsum" so it is shorter, then it also have the same problem – Escobar5 Aug 06 '15 at 20:54
  • This issue has been fixed in `23.1.0` version of support library. – Abhishek V Oct 16 '15 at 06:23

3 Answers3

7

The issue comes to daylight when the ScrollView isn't filled with content. I think it's a very weird bug in Android.

The 'solution'..

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>
Martin Pfeffer
  • 12,471
  • 9
  • 59
  • 68
  • This fixes scrolling but upper elements scrolls beyond the window. – Fedor Kazakov Jul 31 '15 at 10:22
  • Yeah. I guess the question is about the scrolling issue.. If other views appear beyond the window it sounds like there are some other bugs in your config. – Martin Pfeffer Jul 31 '15 at 10:25
  • That's really weird, I guess is an android bug, your solution seems to fix it, unfortunately it makes the views scroll beyond the window as @FedorKazakov says. – Escobar5 Aug 06 '15 at 20:56
  • Ok, that wrong wrapping shouldn't be a too difficult to fix (maybe I find in the next hours some time to fix it) – Martin Pfeffer Aug 06 '15 at 21:09
  • 1
    I've tried to make a proper fix but couldn't. New classes from google are too complicated. It easier to use RecyclerView instead. – Fedor Kazakov Aug 07 '15 at 08:18
  • 1
    In my opinion today(!) the whole lib is garbage. I wasted much time on errors finding/fixing, and at the end I didn't get a proper result. :-/ Maybe it's because I'm not a professional - but after updating to 22.2.1 the setIcon(new Drawable) of the NavigationView isn't working any more. Locgat is firing crap, as well.. very, very weird. Maybe we have to wait for 22.2.x(?)... – Martin Pfeffer Aug 07 '15 at 08:35
  • 2
    Till 23.0.1 this bug still exist!! And There is no source codes very difficult to fix. – DouO Sep 14 '15 at 14:36
0

It seems like the same problem as here: Scroll doesn't work in NestedScrollView when try to scroll from views with click events

This is the kind of workaround for your problem https://stackoverflow.com/a/32887429/2165810

But you shuld also use setPreventCornerOverlap(false) for your CardView to avoid problems with CardView

Community
  • 1
  • 1
smbd uknow
  • 2,282
  • 1
  • 14
  • 22
0

As people have noted, the problem is when the CardView sits fully within the screen it won't trigger scrolling from the card. Scrolling still works from views within the CollapsingToolbarLayout.

My solution was to add android:layout_marginBottom="100dp" to the CardView so that the bottom edge is offscreen thus allowing scrolling.

SoftWyer
  • 1,986
  • 28
  • 33