Android设计库:SwipeRefreshLayout无法检测在CollapsingToolbarLayout上的滑动手势

6

在我的应用程序中,我正在使用CollapsingToolbarLayout跟随NestedScrollViewSwipeRefreshLayout内部。我希望SwipeRefreshLayout能够检测到从CollapsingToolbarLayout上的滑动,但它却只检测从NestedScrollView上的滑动,忽略了CollapsingToolbarLayout上的滑动。以下是我的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>

有人使用过这种东西吗?
1个回答

5
AppBarLayout文档所述,它必须是CoordinatorLayout的直接子项。

此视图在CoordinatorLayout中作为直接子项使用时具有重要依赖性。如果您在不同的ViewGroup中使用AppBarLayout,则大部分功能将无法正常工作。

因此,我不确定SwipeToRefreshLayout必须包含在哪里才能与CollapsingToolbar一起使用,因为NestedScrollView也必须是直接子项。
这里有另一个答案,可以帮助连接SwipeRefreshLayout,并仅在未折叠appBar时激活它,但他们没有使用NestedScrollView,这对我无效 :(
编辑:我找到了解决我的问题的方法:我使用的ListView必须设置为嵌套滚动而不是将其放在NestedScrollView中,或者替换为RecycleView。
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
     listView.setNestedScrollingEnabled(true);
}

那么,解决您问题的方法应该是将NestedScrollView包装在SwipeRefreshLayout中。


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