简体   繁体   中英

How to dynamically change Toolbar from activity inside ViewPager (TabLayout)

I'm struggling with Toolbar Button and ViewPager with TabLayout inside my app. 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. 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? (Changing dynamically the icon on my Toolbar inside the Fragment)

Thanks!

Try implementing the logic inside the onResume of your Fragment. onResume basically means that this fragment is visible for the user. This should solve your problem. Hope it helps:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM