My question is in my activity or fragment, there are TabLayout and ViewPager. If I was to disable swipe of ViewPager by using a class called NonSwipeableViewPager. Then why should I bother to using ViewPager right from the start, I could have just use TabLayout to add tabs which is much more simpler.
What are the other advantages of using ViewPager even though the primary function of it which is Creating Swipe Views with Tabs is not needed?
Do Fragment layout must use framelayout? Which means can I use relative or liner layout instead of framelayout?
Last but not least, do I allowed to use ViewPager with the swipe disabled?
Related
I'm learning about tab layout because I need to use tab layout.
When i search for tab layout on Google, the one that comes up together is the view pager.
By the way, I'm not sure about the difference between tablayout and viewpager.
The tab layout has a menu at the top and screen switching is possible.
Although the view pager does not have tabs, it is possible to slide the screen just like the tab layout.
But why use the two together?
What's different?
Isn't it possible to slide in tab layout as well?
Yes, you can use TabLayout without viewPager. But without viewpager you won't be able to slide your page. You have to handle tabselect listener manually using this method then you have to click on the tab to navigate.
addOnTabSelectedListener(OnTabSelectedListener)
Likewise, you can use only Viewpager then you will be able to slide or swipe your page without any tabs.
From official doc
TabLayout
TabLayout provides a horizontal layout to display tabs.
Viewpager
Layout manager that allows the user to flip left and right through pages of data.
So Tablayout and viewpager are two different things but you can have the privilege of both tabs and slide only by combining them.
You can check here for tablaout without viewPager
TabLayout without using ViewPager
Is it possible to have for each Fragment inside a ViewPager different ViewGroups in the CollapsingToolbarLayout/Toolbar?
I would like to be able to swipe between pages and have different controls inside CollapsingToolbarLayout that interact with their respective Fragmentin the ViewPager, so that they can be hidden while scrolling each fragment inside the ViewPager.
Regards.
When using a ViewPager, is there any advantage to using TabLayout instead of PagerTabStrip or vice versa? Or are they equivalent? Is one more convenient than the other? Does one offer greater functionality? Again all questions are asked, and thus all answers are expected, in the context of ViewPager. Thanks.
TabLayout is a material concept that replaced the deprecated ActionBar tabs in Android 5.0.
It extends HorizontalScrollView, so you can keep adding tabs horizontally which can include text, icons, or custom views and scroll through them linearly without paging.
TabLayout provides the setupWithViewPager(ViewPager viewPager) method to attach to a ViewPager instead of being part of the ViewPager like the PagerTabStrip.
A PagerTabStrip is more of an indictor for the current page of a ViewPager, and "it is intended to be used as a child view of a ViewPager widget". The scrolling for it does not act like the TabLayout since each tab is part of the page instead of individually horizontally scrollable.
I have seen lots of examples of how to use a ViewPager to scan through multiple instances of the same Fragment. I don't want to do that. I want to have a single instance of a Fragment that runs off the edge of a screen and use a ViewPager (or at least the nice clean ViewPager swiping effect) to scan through the different sections of the single fragment. Is this possible?
Is this possible?
You can:
Use nested fragments for the pages in ViewPager, or
Use views for the pages in ViewPager, such as in this sample app, or
Use HorizontalScrollView instead of ViewPager to wrap around a single wide view
I have a FragmentActivity which can swipe through several fragments via ViewPager and a FragmentActivity which hosts an ActionBar for navigation.
Now I want to nest the FragmentActivity with the ViewPager in the first fragment of the other FragmentActivity which hosts the ActionBar.
In other words I like to have an ActionBar navigation and in it's first Tab I'd like to have swipeable fragments.
My problem is how to nest one FragmentActivity in another FragmentActivity. Both work on their own but I don't get them to work together.
Swipe Gesture applied at Fragment level along with ViewPager with it's default swipe disabled
You can write your own touch interceptor for the fragments inside your view-pager.
However on a second opinion, please see my moqup in the question:
You can have several ViewGroup container preferably FrameLayout, where on each you can add or remove fragments using transactions. The container of the Navigation Panel is different than where the ViewPager is showing:
Keep this ViewPager in a separate fragment that spawns in yet another FrameLayout. in ViewPager here you should not use the FragmentPageAdapter, but a regular PagerAdapter (avoid nesting of fragments). Hence the ViewPager is a parent container for the Views, and not Fragments where they are shown.
The target of the navigation drawer (on item click) will be this second FrameLayout. Keep it simple!!
Similarly you can have a ViewPager inside a first tab of Tab based ViewPager with this new found information.