Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Design Library: SwipeRefreshLayout don't detect swipe over CollapsingToolbarLayout

In my app I am using CollapsingToolbarLayout following NestedScrollView inside SwipeRefreshLayout. What I want from SwipeRefreshLayout is to detect swipe over from CollapsingToolbarLayout, but its detecting over from NestedScrollView and ignoring swipe on CollapsingToolbarLayout. Here is my XML:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout 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.design.widget.CoordinatorLayout
        android:id="@+id/rootLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.SwipeRefreshLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            android:id="@+id/swipe"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_behavior="@string/appbar_scrolling_view_behavior">

            <android.support.design.widget.AppBarLayout
                android:id="@+id/appBarLayout"
                android:layout_width="match_parent"
                android:layout_height="256dp"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

                <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsingToolbarLayout"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:contentScrim="?attr/colorPrimary"
                    app:expandedTitleMarginStart="@dimen/expanded_toolbar_title_margin_start"
                    app:layout_scrollFlags="scroll|exitUntilCollapsed">

                    <ImageView
                        android:id="@+id/profilePic"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:scaleType="centerCrop"
                        android:src="@drawable/ic_split_big_profile"
                        app:layout_collapseMode="parallax"
                        app:layout_collapseParallaxMultiplier="0.7" />

                    <include
                        android:id="@+id/toolbar_layout"
                        layout="@layout/toolbar"
                        android:layout_width="match_parent"
                        android:layout_height="?attr/actionBarSize"
                        app:layout_collapseMode="pin"
                        app:layout_scrollFlags="scroll|enterAlways" />

                </android.support.design.widget.CollapsingToolbarLayout>
            </android.support.design.widget.AppBarLayout>

            <android.support.v4.widget.NestedScrollView
                android:id="@+id/transactions_scroll"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <RelativeLayout
                    android:layout_width="match_parent"
                    android:layout_height="500dp">
                </RelativeLayout>
            </android.support.v4.widget.NestedScrollView>

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/add_button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="bottom|right"
                android:layout_marginBottom="@dimen/fab_margin_bottom"
                android:layout_marginRight="@dimen/fab_margin_right"
                android:onClick="addTxn"
                android:src="@drawable/ic_action_add_transaction_light"
                app:elevation="6dp"
                app:fabSize="normal" />

        </android.support.v4.widget.SwipeRefreshLayout>
    </android.support.design.widget.CoordinatorLayout>
</RelativeLayout>

Has anybody used this sort of thing?

like image 870
Dr. aNdRO Avatar asked Aug 18 '15 10:08

Dr. aNdRO


1 Answers

As stated in AppBarLayout docs, it has to be direct child of CoordinatorLayout

This view depends heavily on being used as a direct child within a CoordinatorLayout. If you use AppBarLayout within a different ViewGroup, most of it's functionality will not work.

Thus, I'm not sure where the SwipeToRefreshLayout has to be included to work together with the CollapsingToolbar, as NestedScrollView has to be direct child too.

Here is another response that helps to attach the SwipeRefreshLayout and activate it only when the appBar is uncollapsed, but they are not using a NestedScrollView, which doesn't work for me :(


EDIT I found a solution for my problem: The ListView I used, had to be set as nested scroll instead of put it inside a NestedScrollView, or replaced with RecycleView:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     listView.setNestedScrollingEnabled(true);
}

So, a solution for your issue should be to wrap the NestedScrollView inside the SwipeRefreshLayout.

like image 60
Sergio Álvarez Avatar answered Oct 23 '22 03:10

Sergio Álvarez