如何从Android支持库中锁定CollapsingToolbarLayout

urg*_*as9 5 android android-fragments android-fragmentactivity android-collapsingtoolbarlayout

我正在使用带有(通常)一个片段作为内容的Activity类。在活动中,我CollapsingToolbarLayout用作某些信息的标头,并且一切正常。但是在某些情况下(当附加了一些片段时),我不想显示该信息,也不想CollapsingToolbarLayout滚动打开。

我要实现的是锁定CollapsingToolbarLayout,防止它从片段中打开。我正在以编程方式将其折叠appBarLayout.setExpanded(false, true);

Kuf*_*ffs 5

我想出了另一种方法,因为设置嵌套滚动标记仅在拖动NestedScrollView时才有效。仍然可以通过在应用栏上滑动来扩展应用栏。

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

此功能假定您从扩展的工具栏开始

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);
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 注意!不要忘了调用以下行:工具栏.setLayoutParams(lp); 否则,解决方法将不起作用。 (2认同)

urg*_*as9 4

好吧,我自己解决了。技巧是禁用嵌套滚动行为ViewCompat.setNestedScrollingEnabled(recyclerView, expanded);

当我使用活动中的一个片段作为内容视图并将其放在后台堆栈上时,我只需检查后台堆栈何时更改以及哪个片段可见。请注意,我在每个片段中使用 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);
                }
            }
        }
    });
Run Code Online (Sandbox Code Playgroud)

该线程对我帮助很大,其中提出了另一种可能的解决方案: Need to disable Expand on CollapsingToolbarLayout for certainfragment