11

我在我的项目中使用以下布局。它工作正常,但即使 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>
4

4 回答 4

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>
于 2018-06-20T04:38:12.630 回答
0

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

com.android.support:design:22.2.1
于 2015-07-29T13:24:04.397 回答
0

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

首先,要启用/禁用滚动,我们必须实现一个自定义LinearLayoutManager(感谢这篇文章):

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);
}
于 2017-12-15T09:44:40.390 回答
-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">

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

于 2015-07-21T07:08:35.417 回答