简体   繁体   English

在CoordinatorLayout上以编程方式隐藏/显示工具栏

[英]Hide/Show Toolbar programmatically on CoordinatorLayout

When I scroll my RecycleView ToolBar hide or show (with animation). 当我滚动我的RecycleView ToolBar隐藏或显示(带动画)时。 在此输入图像描述

How I can return ToolBar back programmatically? 我如何以编程方式返回ToolBar

If your toolbar is inside an AppBarLayout which is probably inside your CoordinatorLayout then something like this should work. 如果您的工具栏位于AppBarLayout内,该AppBarLayout可能位于您的CoordinatorLayout内,那么这样的东西应该可行。

AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBar);
            appBarLayout.setExpanded(true, true);

Or to collapse it 或者崩溃它

AppBarLayout appBarLayout = (AppBarLayout)findViewById(R.id.appBar);
            appBarLayout.setExpanded(false, true);

Here is the definition 这是定义

setExpanded(boolean expanded, boolean animate)

Take note that this method is available from v23 of the support library, here is some documentation for reference, the key thing to note is " As with AppBarLayout's scrolling, this method relies on this layout being a direct child of a CoordinatorLayout. " Hope this helps! 请注意,此方法可从支持库的v23获得,这里有一些文档供参考,需要注意的关键是“ 与AppBarLayout的滚动一样,此方法依赖于此布局是CoordinatorLayout的直接子项。 ”希望这样帮助!

Is that what you looking for? 这就是你要找的?

Toolbar toolbar = findViewById(R.id.toolbar);  // or however you need to do it for your code
AppBarLayout.LayoutParams params = (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(0);  // clear all scroll flags

link: How to enable/disable toolbar scrolling programmatically when using design support library 链接: 如何在使用设计支持库时以编程方式启用/禁用工具栏滚动

In order to hide the Toolbar your can just do something like this: 为了隐藏工具栏,您可以执行以下操作:

toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();

If you want to show it again you call: 如果您想再次显示它,请致电:

toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();

My problem was very similar to @Artem I tried many fix but none of them worked for me. 我的问题与@Artem非常相似我试过很多修复,但没有一个对我有效。 @Jraco11's answer is correct when you use AppBarLayout . 使用AppBarLayout时,@ Jraco11的答案是正确的。 @johnrao07 not worked for me. @ johnrao07不适合我。 But I found a perfect solution for this problem when we use Toolbar . 但是当我们使用Toolbar时,我找到了解决这个问题的完美解决方案。

To hide Toolbar programatically 以编程方式隐藏工具栏

if (toolbar.getParent() instanceof AppBarLayout){
                    ((AppBarLayout)toolbar.getParent()).setExpanded(false,true);
                }

To show Toolbar programatically 以编程方式显示工具栏

if (toolbar.getParent() instanceof AppBarLayout){
                        ((AppBarLayout)toolbar.getParent()).setExpanded(true,true);

Refer original answer(answer by @Android HHT):- programmatically-show-toolbar-after-hidden-by-scrolling-android-design-library 请参阅原始答案(由@Android HHT回答): - 以 编程方式显示工具栏 - 隐藏 - 滚动 - 安卓 - 设计 - 库

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用CoordinatorLayout滚动隐藏工具栏 - Hide Toolbar on scroll with CoordinatorLayout android - CoordinatorLayout / NestedScrollView /隐藏 - 显示工具栏/ WebView问题 - android - CoordinatorLayout/NestedScrollView/Hide-Show Toolbar/Issue with WebView 我无法在CoordinatorLayout中隐藏工具栏 - I can not hide Toolbar in CoordinatorLayout 使用CoordinatorLayout隐藏工具栏,但在片段上使用RecyclerView - Hide Toolbar with CoordinatorLayout, but RecyclerView on a fragment 没有 CoordinatorLayout 的编程可折叠/可扩展工具栏? - Programmatically collapsible/expandable toolbar without CoordinatorLayout? 使用CoordinatorLayout从片段中隐藏活动中的工具栏 - Hide Toolbar in activity from fragment using CoordinatorLayout 工具栏不会在CoordinatorLayout中滚动时隐藏/显示 - Toolbar does not Hide/Shown on Scrolling in CoordinatorLayout 滚动时隐藏工具栏无法正常工作[CoordinatorLayout] - Hide Toolbar on scroll wont work properly [CoordinatorLayout] 如何在CoordinatorLayout中向工具栏显示固定视图 - How to show pinned view to Toolbar in CoordinatorLayout 仅在CoordinatorLayout中滚动时显示工具栏阴影 - Show Toolbar shadow only on scroll in CoordinatorLayout
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM