I have a viewpager with 2 fragments. In the second fragment, I am displaying snackbar (using a view in the fragment). I would like to hide that snackbar when the user swipes to the first tab. I don't know why if the bar is being displayed in a layout of a fragment, I can see it in other fragment.
For viewpager when user swipes from one screen to another screen(fragment)
ViewPager.OnPageChangeListener() will gets invoked you can handle here.
Use CoordinatorLayout for your fragment.
As per android documentation:
"The method actually searches up the view hierarchy from the passed view until it reaches either a CoordinatorLayout, or the window decor's content view"
Using coordinatorLayout for your fragment will attach the snackbar to the fragment, so when you swipe it wont overlap with other fragment.
If your fragment is using other than CoordinatorLayout, enclose your layout with CoordinatorLayout.
Documentation link https://developer.android.com/training/snackbar/showing
Related
I have a CoordinatorLayout that contains a BottomNavigationView and an AppBarLayout with a ToolBar inside of it. (The BottomNavigationView is not inside the AppBarLayout, as doing it breaks the position of the BottomNavigationView).
I need to show/hide the AppBarLayout and the BottomNavigationView programmatically, when certain events happen (e.g. on a fragment's onResume), and so far I've managed setting appBarLayout.setExpanded(true, true) in order to show/hide the AppBar, but I can't figure out how to do the same for the BottomNavigationView, as it does not have any method to show/hide itself.
The behaviour of my BottomNavigationView is app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior", set in the layout xml. How can I get a reference to this behaviour in my code in order to manage its expanded/collapsed state?
Thats code slides up your bottom navigation that are inside of a Coordinator Layout and have a HideBottomViewOnScrollBehavior.
If you are using findViewById just replace that for every binding.
val layoutParams = binding.bottomNavigation.layoutParams as CoordinatorLayout.LayoutParams
val bottomViewNavigationBehavior = layoutParams.behavior as HideBottomViewOnScrollBehavior
bottomViewNavigationBehavior.slideUp(binding.bottomNavigation)
You can hide bottomNavigation view from your fragment container activity. Because of you have bottomNavigation id. and you can access this id from your fragment container activity and also you can get the current fragment instance from your fragment container activity. So from your example if you want to hid bottom navigation from particular fragment onResume() event you can check that which fragment is currently visible from you Fragment container activity and call hide or show bottom navigation.
Remember for hiding or showing bottom navigation use animation.
I have a Tablayout kind of a bottom navigation, and I want that when the user clicks in a cardview that I have it sends him to another Tablayout but when that happens the bottom navigation does not appear anymore. That is only possible with activities right?
A TabLayout is just a view like any other. It can be inflated as a part of another view, or as the content view for a Fragment or an Activity. So, you're not restricted to using just an Activity with a TabLayout.
Now, if you want it so that when you click on a CardView you're taking to another screen without a TabLayout, you have a couple of options:
Open a new activity
Since this second Activity is going to have a different content view, it's not going to have the TabLayout from the first activity.
Hide the TabLayout and change the content surrounding the CardView
Since we're sticking with the first Activity, we're going to need to make all of our content changes to the views in the Activity. This means setting the visibility on the TabLayout to View.GONE and potentially lots of changes to the rest of the views depending upon what your layout contains.
I noticed that you didn't mention a ViewPager at all. Typically, this is what will be used with a TabLayout to swap between Fragments when you click on each tab. You could have all of your tabs' content in separate Fragments and then when you click on a CardView, just swap that tab's Fragment out for a different one and hide the TabLayout.
So, to answer your question, it is easier to just open a new activity, but it is possible to not use a second activity if you want to put in the work to modify your first one in runtime.
What i am doing is showing Fragment in view pager and has Gesture detector implemented on Whole view. When we touch the View i want to hide some layouts from current fragment and all other fragments in view pager.
But problem is this that view pager automatically generates next and previous view
and when i swipe to next view it does not hide those layouts but other Fragments other than its consecutive ones Layouts are Gone.
How do i refresh consecutive fragments of view pager from fragment itself.
Thanx in advance.
You should have an activity that control that ViewPager and detects what Fragments to display. That's the place to update the fragments.
Use the OnPageChangeListener listener and inform the fragment of the change.
If you want to do it from the Fragment itself, try onStart() or onResume()
Fragments can be notified of events, so they can take actions accordingly.
One way of doing this would be using a broadcast like in ViewPager Activity to notify a Fragment of a specific event
Other way is like #Antonio said, making it on the OnPageChangeListener (I suppose that would be a lot cleaner).
I have a ViewPager with a FragmentPageAdapter set for it.
And the ViewPager is show some simple fragment.
I am able to use ViewPager for swipe between fragments.
However, I don't like the scrolling effect of ViewPager that showing partial content of next fragment while swiping/dragging current fragment.
Instead, I would like to switch to next fragment at instant with swipe(but without scrolling effect) and show fragment immediately.
How can I do such thing(must use ViewPagger)?
You have to implement your own "SwipeDetector" and use mViewPager.setCurrentItem(index, false) method (second parameter indicates whether to enable animation or not).
I have a ZooFragment which contains a ViewPager.
This ViewPager has three children: LionFragment, LeopardFragment, and TigerFragment, each of these children can request transaction to call a new ZooFragment.
When a ZooFragment called zooA (with arguments) is initialized, all three children in ViewPager display content. From any child fragment, user touch will call a new ZooFragment called zooB (with different arguments, of course).
Based on transaction action from child fragment to ZooFragment:
1.If I use transaction.replace(), zooB will be blank, all three children in zooB display no content, empty. At zooB, pressing hardkey Back from navigation, zooA becomes empty.
2.If I use transaction.add(), zooB won't be blank, following by a Back button press, zooA gets empty.
In ZooFragment class, I do loading data in onCreateView(), so what is the reason why all the child fragments in ViewPager get empty?
Please do not replace the fragments in the ViewPager, just show (using transaction) in any other contained in the same layout as that of ViewPager and that container should be a FrameLayout. Also device a mechanism to know when and what fragment comes into view. Then request for a fresh data from a utility that responds to update your fragment. I do not think onDestroyed will be called in case of all the fragments in the ViewPager.
Fragments in the ViewPager are fixed, so should there contents will not go till the application manager wants to destroy it.Instead of trying to replace the fragments in the adapter, try to give a different set of fragments and notifyDataSet changed, or take the advantage of FrameLayout to show another fragment over the view pager tab's current fragment.
There is my solution that works:
Swipe Gesture applied at Fragment level along with ViewPager with it's default swipe disabled