如何以编程方式为工具栏设置 app:layout_scrollFlags [英] How to set app:layout_scrollFlags for Toolbar programmatically

查看:41
本文介绍了如何以编程方式为工具栏设置 app:layout_scrollFlags的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序有一个 viewpager 和 4 个标签,每个标签都有很多片段.但我只希望我的工具栏在 2 个特定选项卡中滚动 recyclerview 时向上/向下滚动.但我不知道如何阻止其他选项卡的工具栏滚动.我试图为每个片段导入工具栏,但似乎我做不到.有人有解决这个问题的想法吗?

My app have a viewpager and 4 tabs, each tab have many fragment. But I just want my Toolbar scroll up/down while scrolling recyclerview in 2 specific tabs. But I don't know how to block Toolbar scroll for other tabs. I tried to import toolbar for each fragment but it seems I can't do it. Anyone have idea to solve this problem?

推荐答案

我强烈建议不要根据选择的选项卡更改滚动标志 - 在滚动到某个选项卡时让工具栏自动返回(并且内容向下移动)非 recyclerview 选项卡可能非常刺耳,而且可能不是您想要的交互模式(如果您的两个 RecyclerView 选项卡彼此相邻,则会被激怒).

I'd strongly recommend against changing the scrolling flags based on what tab is selected - having the Toolbar automatically return (and the content move down) when scrolling to a non-recyclerview tab can be very jarring and probably not an interaction pattern you want (exasperated if your two RecyclerView tabs are next to one another).

但是,如果您想亲自查看,可以使用 setScrollFlags() 以编程方式设置滚动标志:

However, if you want to see it in person, you can use setScrollFlags() to set the scroll flags programmatically:

Toolbar toolbar = ... // your toolbar within an AppBarLayout
AppBarLayout.LayoutParams params = 
    (AppBarLayout.LayoutParams) toolbar.getLayoutParams();
params.setScrollFlags(AppBarLayout.LayoutParams.SCROLL_FLAG_SCROLL
    | AppBarLayout.LayoutParams.SCROLL_FLAG_ENTER_ALWAYS);

为了清除标志

params.setScrollFlags(0)

这篇关于如何以编程方式为工具栏设置 app:layout_scrollFlags的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆