当RecyclerView为空时,CollapsingToolbarLayout不应该折叠。

12

我在项目中使用以下布局。虽然工作正常,但是当RecyclerView为空或者RecyclerView只有很少的项时,CollapsingToolbarLayout仍然会折叠。 我想要的行为是,当RecyclerView的项数大于可见项数时,CollapsingToolbarLayout才会折叠。如何实现这种行为?

<?xml version="1.0" encoding="utf-8"?>
    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_layout"
        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="wrap_content"
        android:fitsSystemWindows="true">

        <android.support.design.widget.AppBarLayout
            android:id="@+id/app_bar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fitsSystemWindows="true"
            android: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"
                android:fitsSystemWindows="true"
                app:contentScrim="?attr/colorPrimary"
                app:layout_scrollFlags="scroll|exitUntilCollapsed">

                <include
                    layout="@layout/header"
                    android:fitsSystemWindows="true"
                    app:layout_collapseMode="parallax"/>

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

        <android.support.v7.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </android.support.design.widget.CoordinatorLayout>

嗨Vivart,我也遇到了同样的问题。如果你已经解决了这个问题,请分享你的想法。谢谢。 - Murali Ganesan
我也实现了同样的功能,但不幸的是我的折叠BarLayout从未折叠过。请看我的问题 - http://stackoverflow.com/questions/33093066/collapsing-bar-layout-and-recyclerview - Aman Verma
4个回答

0
如果这仍然相关,我认为我已经成功实现了所需的行为。
首先,要启用/禁用滚动,我们必须实现自定义LinearLayoutManager(感谢this post):
public class CustomLayoutManager extends LinearLayoutManager {

    private boolean isScrollEnabled = true;

    public CustomLayoutManager(Context context) {
        super(context);
    }

    public void setScrollEnabled(boolean flag) {
        this.isScrollEnabled = flag;
    }

    @Override
    public boolean canScrollVertically() {
        return isScrollEnabled && super.canScrollVertically();
    }
}

并将其设置为 RecyclerView

layoutManager = new CustomLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);

然后我们需要检测何时启用和何时禁用滚动。我使用了一个AppBarLayout.OnOffsetChangedListener来实现:

appBarLayout = mainView.findViewById(R.id.app_bar_layout);
appBarLayout .addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        // verticalOffset == 0 means that the AppBarLayout is expanded
        if (verticalOffset == 0) {
            // here we check that the last item of the RecyclerView is visible
            if (viewIsVisible(layoutManager.findViewByPosition(layoutManager.getItemCount() - 1))) {
                layoutManager.setScrollEnabled(false);
            } else {
                layoutManager.setScrollEnabled(true);
            }
    }
    }
});

这里是检查视图是否可见的方法:

private boolean viewIsVisible(View view) {
    Rect scrollBounds = new Rect();
    list.getHitRect(scrollBounds);
    return view != null && view.getLocalVisibleRect(scrollBounds);
}

0
<android.support.design.widget.CoordinatorLayout
android:id="@+id/coordinator_layout"
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="wrap_content"
android:fitsSystemWindows="true">

    <android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:fitsSystemWindows="true"
    android: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"
        android:fitsSystemWindows="true"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <include
            layout="@layout/header"
            android:fitsSystemWindows="true"
            app:layout_collapseMode="parallax"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            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:layout_width="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
android:layout_height="match_parent">

 <android.support.v7.widget.RecyclerView
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:nestedScrollingEnabled="false"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

  </android.support.v4.widget.NestedScrollView>

</android.support.design.widget.CoordinatorLayout>

android:nestedScrollingEnabled="false" 是必须的,或者使用 recyclerview.setNestedScrollingEnabled(false); - Praveen

0

这是一个已知的 bug。请将您的支持库更新至 22.2.1 版本:

com.android.support:design:22.2.1

3
版本23.1.0仍存在该问题。 - Longi
有人有这个 bug 的链接吗? - Eefret
3
仍然存在这个 bug...在 23.1.1 版本中。 - Blunderer

-1
尝试将你的collapsingBarLayout xml更改为:
 <android.support.design.widget.CollapsingToolbarLayout
                    android:id="@+id/collapsing_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:fitsSystemWindows="true"
                    app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways|enterAlwaysCollapsed">

我已经添加了两个额外的标志:enterAlways和enterAlwaysCollapsed。这不是一个百分之百有效的解决方案,但比您目前拥有的行为更好。


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