[英]How to dynamically change Toolbar from activity inside ViewPager (TabLayout)
I'm struggling with Toolbar Button and ViewPager with TabLayout inside my app.我在我的应用程序中使用 TabLayout 使用 Toolbar Button 和 ViewPager。 I have multiple Toolbar Buttons, that I want to change dynamically (Add or remove icon/button on Toolbar, and set different onClickListener for each Fragment) inside Fragment loaded inside TabLayout.
我有多个工具栏按钮,我想动态更改(添加或删除工具栏上的图标/按钮,并为每个片段设置不同的 onClickListener)在 TabLayout 中加载的片段内。 The main problem here is that ViewPager load 2 fragments in a row in order to provide the scroll animation, but there is no method in order to know if the fragment is really displayed or not... (Except setUserVisibleHint, but this method is deprecated, and doesn't seems to be really reliable), so does someone have a solution in order for my app to run great with that behaviour?
这里的主要问题是ViewPager连续加载2个片段以提供滚动animation,但是没有方法可以知道片段是否真的显示......(除了setUserVisibleHint,但不推荐使用此方法,并且似乎并不真正可靠),那么有人有解决方案来让我的应用程序在这种行为下运行良好吗? (Changing dynamically the icon on my Toolbar inside the Fragment)
(动态更改片段内工具栏上的图标)
Thanks!谢谢!
Try implementing the logic inside the onResume of your Fragment.尝试在 Fragment 的 onResume 中实现逻辑。 onResume basically means that this fragment is visible for the user.
onResume 基本上意味着这个片段对用户是可见的。 This should solve your problem.
这应该可以解决您的问题。 Hope it helps:)
希望能帮助到你:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.