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

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 :)

Related

BottomSheetDialogFragment behaviour disorder when added nested fragment in Android

I've a BottomSheetDialogFragment inside a RecyclerView, when is clicked a button BottomSheet is showing. BottomSheetDialog expanding when scrolling down and collapsing when scroll finished on top normally.
But when I place the RecyclerView inside a fragment and lay the fragment to BottomSheetDialog; BottomSheet behaviours not working on the fragment, only works RecyclerView scrolling. On the other hand the behaviours work other places on BottomSheet. How to enable the behaviours as in the first?
You should check that your container layout has app:layout_behavior="#string/bottom_sheet_behavior"> and check that any listener/event you define extra, otherwise BottomSheetDialog normally behaive as you expect.

Toolbar disappears upon transition to new Fragment

There are two fragments - RuleListFragmetn and RuleFragment. The RuleListFragment fragment uses the RecyclerView element. Toolbar has app flags: layout_scrollFlags = "scroll | enterAlways | snap", while AppBarLayout has app parameter: liftOnScroll = "true". If the list in RuleListFragment has not been scrolled and the transition to the RuleFragment fragment has been completed, then the Toolbar is in place. If the list in the RuleListFragment has been scrolled (the Toolbar hid at the top), then when you go to the RuleFragment Toolbar fragment, it disappears. The picture below explains. Maybe in the fragment you need to reset the position of the Toolbar? But how to do that?
Your supposition is correct. As the appBarLayout lives in the activity, the toolbar stays hidden when you change fragments.
The solution is to call appBarLayout.setExpanded(true) in the new fragment. You can do it inside onStart, for example.
More information here: AppBarLayout.setExpanded(boolean expanded).

Dynamically adding tabs to ViewPager with animation

Ive been looking around for quite a while but i cannot seem to find anything about this.
Would it be possible to animate adding a new tab to the viewpager?
I am able to add a new fragment to my pagerAdapter and call notifyDataSetChanged() to dynamically add of remove view from the TabLayout. However, I bet it would look much better if its animated.
Is this possible?
What apProach would be recommended for this?

Android ViewPager Fragments - No Swipe and Preloading?

I have a pretty standard implementation of ViewPager using Fragments; however I'm wondering if it's possible to get rid of the preloading of tabs, and to only load a tab activity once user clicks on it?
Also, can we get rid of the swipe effect, just like in iOS?
Thank you
If you want the user to only click on tabs, then don't use a ViewPager; put the views in a FrameLayout, make them all invisible and only set the view that is selected visible. When the user clicks on a tab, make the current view invisible and selected view visible. Much simpler than implementing a PagerAdapter.

Tabs on top and bottom of the screen

I'm trying to build a view that will have standard tabs on the bottom and action bar tabs in the upper part of the screen.
I've tried using FragmentTabHost together with TabLayout but with no success since they both call getSupportFragmentManager() so one cancels another.
I've also thought about using deprecated TabLayoutActivity but I'm using AppCompat theme so all my activities have to inherit AppCompatActivity.
Is there a way for bottom tab to start activities and upper one fragments, or at least both starting fragments that work?
You can have 2 framelayouts (bottom,middle).
On bottom framelayout you will have a fragment with buttons as tabs, when you click on each button you will send back to activity which button clicked.
On middle framelayout you can have a TabLayout fragment and if you prefer also 1 fragment for each tab in the tablayout.
One of the solutions is to use getChildFragmentManager() available in the support library to have a Fragment inside another Fragment.
The outer one with FragmentTabHost can be controlled by main fragment manager getFragmentManager() and inner one with TabLayout by child fragment manger getChildFragmentManager().
Here's an example with nested fragments.
Still looking for a viable solution with Activity and a Fragment.

Categories