如何从 Android 支持库中锁定 CollapsingToolbarLayout

How to lock CollapsingToolbarLayout from Android support library

我正在使用 Activity class 和(通常)一个片段作为内容。在 Activity 中,我使用 CollapsingToolbarLayout 作为某种 header 来获取一些信息,并且一切正常。但在某些情况下(当附加了一些片段时)我不想显示该信息,我不想 CollapsingToolbarLayout 在滚动时打开。

我想实现的是锁定CollapsingToolbarLayout,防止它从片段中打开。我正在使用 appBarLayout.setExpanded(false, true);

以编程方式折叠它

好吧,我设法自己解决了。诀窍是使用 ViewCompat.setNestedScrollingEnabled(recyclerView, expanded);

禁用嵌套滚动行为

因为我使用 activity 中的一个片段作为内容视图并将其放在后台堆栈中,所以我只是检查后台堆栈何时更改以及哪个片段可见。请注意,我在每个片段中使用 NestedScrollView 来触发可折叠工具栏。这是我的代码:

getSupportFragmentManager().addOnBackStackChangedListener(new FragmentManager.OnBackStackChangedListener() {
        @Override
        public void onBackStackChanged() {
            NestedScrollView nestedScrollView = (NestedScrollView)findViewById(R.id.nested_scroll_view);
            int size = getSupportFragmentManager().getBackStackEntryCount();
            if (size >= 1 && nestedScrollView != null) {
                if (getSupportFragmentManager().getBackStackEntryAt(size - 1).getName().equals("SpotDetailsFragment")) {
                    Log.d(LOG_TAG, "Enabling collapsible toolbar.");
                    ViewCompat.setNestedScrollingEnabled(nestedScrollView, true);
                } else {
                    Log.d(LOG_TAG, "Disabling collapsible toolbar.");
                    ViewCompat.setNestedScrollingEnabled(nestedScrollView, false);
                }
            }
        }
    });

这个帖子对我帮助很大,其中提出了另一种可能的解决方案:

我想出了一个不同的方法,因为设置嵌套滚动标志仅在拖动 NestedScrollView 时有效。仍然可以通过在栏本身上滑动来展开应用栏。

我在 "Utils" class 中将其设置为静态函数。显然,您在解锁时设置的标志将取决于哪些标志与您的用例相关。

此函数假定您从展开的工具栏开始

public static void LockToolbar(boolean locked, final AppBarLayout appbar, final CollapsingToolbarLayout toolbar) {

    if (locked) {
        // We want to lock so add the listener and collapse the toolbar
        appbar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {

            @Override
            public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
                if (toolbar.getHeight() + verticalOffset < 2 * ViewCompat.getMinimumHeight(toolbar)) {
                    // Now fully expanded again so remove the listener
                    appbar.removeOnOffsetChangedListener(this);
                } else {
                    // Fully collapsed so set the flags to lock the toolbar
                    AppBarLayout.LayoutParams lp = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
                    lp.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED);
                }
            }
        });
        appbar.setExpanded(false, true);
    } else {
        // Unlock by restoring the flags and then expand 
        AppBarLayout.LayoutParams lp = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
        lp.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL | AppBarLayout.LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED);
        appbar.setExpanded(true, true);
    }

}