【问题标题】:CollapsingToolbarLayout should not collapse when RecyclerView is empty当 RecyclerView 为空时,CollapsingToolbarLayout 不应折叠
【发布时间】:2015-09-14 19:34:46
【问题描述】:

我在我的项目中使用以下布局。它工作正常,但即使 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,我面临同样的问题。如果您有解决问题,请分享您的想法。谢谢
  • 我也实现了相同的,但不幸的是我的折叠栏布局没有折叠。曾经。看看我的问题 - stackoverflow.com/questions/33093066/…

标签: android android-recyclerview androiddesignsupport android-collapsingtoolbarlayout


【解决方案1】:

这是一个已知的错误。将您的支持库更新到 22..2.1:

com.android.support:design:22.2.1

【讨论】:

  • 23.1.0 版中仍然存在问题。
  • 有人有错误链接吗?
  • 23.1.1 仍然有这个错误...
【解决方案2】:

如果这仍然相关,我想我设法实现了预期的行为。

首先,要启用/禁用滚动,我们必须实现自定义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);
}

【讨论】:

    【解决方案3】:
    <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);
    【解决方案4】:

    尝试将 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">
    

    我添加了 2 个附加标志:enterAlways 和 enterAlwaysCollapsed。这不是一个 100% 有效的解决方案,但比您当前的行为要好。

    【讨论】:

      猜你喜欢
      • 2020-06-13
      • 1970-01-01
      • 1970-01-01
      • 2016-02-01
      • 2016-03-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多