I'm want to make a ViewPager with TabLayout, 5 tabs and fragments in all. When the Activity is created, I only want the FragmentPagerAdapter to create the first (1st) tab, and then create the others when the user selects those tabs. setUserVisibleHint() does not work for my use case. I have also tried setting a dummy adapter first.
It cannot be done with viewpager
the default value of loaded screens outiside the viewpager is 1 and you can change it by calling
viewPager.setOffscreenPageLimit(1);
however the minimum value of the offscreenLimit is 1.
Hope it helps.
Related
I have a tablayout within a viewpager and would like to listen for tab changes. Which listener should I use? The first seems to work fine but then what is the purpose of the second one?
The docs say that the second listener provides callbacks to the viewpager but it doesn't make it clear what that means/why it's necessary or a good idea.
A TabLayout.OnTabSelectedListener class which contains the necessary calls back to the provided ViewPager so that the tab position is kept in sync.
Inside TabLayout.OnTabSelectedListener you need to call viewpager to change the page, but in TabLayout.ViewPagerOnTabSelectedListener you can link the TabLayout and the ViewPager with tabLayout.setupWithViewPager(ViewPager), tabLayout.ViewPagerOnTabSelectedListener will pass you the viewpager as parameter each time the user change of tab.
I recommend you use the one with which you feel more comfortable
I build an application which has tabs in it, and using ViewPager to swipe between the tabs.
Is it possible that one tab will show firstFragment, and then when pressing a Button will show firstFragment and secondFragment?
Of coarse you can. When you bind ViewPager to TabLayout you use adapter, which extends FragmentPagerAdapter, that adapter stores list of all fragments and layout texts. Just replace fragment in it and invalidate result.
But, this should by done from Activity, which has ViewPager and TabLayout.
Define interface in your first program, implement it in Activity and replace fragment on button click.
Hey i have 5 Fragments like Fragment1,Fragment2,Fragment3,..Which i am Displaying in a ViewPager But I want Fragment2 should display first instead of Fragment1when i am starting my app.
After setting the adapter for ViewPager, you can simply use setCurrentItem(position) to set the page selected at required position.
mViewPager.setCurrentItem(position);
I'm having a bit of trouble implementing a design based around multiple ViewPagers.
At a high level, I have a FragmentActivity with just a FrameLayout as it's content. I have 3 different Fragments that I want to display. All 3 are full screen and only 1 will be used at a time.
Fragment 1 is a basic fragment with some TextViews and ImageViews.
Fragment 2 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments.
Fragment 3 has a ViewPager and a FragmentPagerAdapter that feeds it several simple fragments (that are different from Fragment 2)
In my FragmentActivity onCreate() I get the FragmentManager and begin a transaction to replace whatever is in my FrameLayout with a new instance of Fragment 2.
At this point everything is working as expected. My ViewPager in Fragment 2 works perfectly.
Now I have a menu option that replaces the Fragment 2 in my FrameLayout with a new instance of Fragment 3. This also works fine.
The problem arises when I try to put Fragment 2 back into the FrameLayout with another replace transaction. I see my PagerIndicater at the top, but my pages are blank.
I've tried just creating a new instance of my Fragment 2 and calling a replace transaction. I've also tried setting a tag on my Fragments when I call replace and adding a findFragmentByTag check before my replace instead of creating a new instance. Both gave me the same result of blank pages after my second replace.
For reference
My first design was simply a FragmentActivity with a ViewPager and a ViewIndicater. I only had Fragment 2 and Fragment 3 from my description above and a menu option to switch between them. To switch I had 2 different FragmentPagerAdapters defined and just called ViewPager.setAdapter to set the selected FragmentPagerAdapter. This was working perfectly, but now I need a new top level Fragment that isn't using ViewPager at all. This is why I decided to move my ViewPagers out into their own Fragments. My idea being that I would just swap in my fragments to a FrameLayout.
I don't have my code in front of me right now so I can't post any, but I'll add some code to my question tomorrow to help facilitate answers.
This question is a possible duplicate of Navigating back to FragmentPagerAdapter -> fragments are empty.
If your app can handle it (API 17), use getChildFragmentManager(). This problem seems to occur when using a Fragment to host your ViewPager and using FragmentPagerAdapter. Changing to FragmentStatePagerAdapter seemed to fix the problem as well, but I still think using getChildFragmentManager() is the smartest thing to do.
brockoli you can used not "good way". But for my it's worked.
You can use in view 2 layouts, where you bind fragments. First - for fragment with fragments. Second - for other fragments.
On replace fragment with fragments do not replace, but only change visibility first layout to gone and add new fragment to second layout. On back, remove fragment from second layout and set visibility for first layout to visible.
In my application I am using ViewPager from the support library- v4
In main screen I have viewPager which got max 5 Fragment, all fragment belongs to one class ArticlePager
Now in main screen there are list on categories, now the content of the pager is based on that selection,
The problem I am having is, I have used FragmentPagerAdapterwhich stores the Fragment and if the fragment is already exist, It will return the old Fragment without recreating it. That things runs perfectly, but Problem Occurs # the time of orientation change.
For instance
If there are 5 View normally in every fragment For the given position, but There are also some which contains 2-3 views. Now if I change the orientation on page No. lets say 5 which contains only 3 view inside.
So, by now in every category on Page 5 I'll got the view containing 3 view, which is not something I want.
In my application each category contains the pagination
Is there any way such that i can destroy and recreate the Fragment on click of category? or any other work around
Thank you
OK thanks to open source I find my solution, FragmentPagerAdapter I have override the method instantiateItem and got the solution.
This can be easily achieved by FragmentStatePagerAdapter such that it doesn't store the fragment. It recreate it all the time, but I don't want that in 100's of page because of only few pages.
So if I understand correctly, your problem is that after rotation, the wrong set of fragments are in you ViewPager?
Why don't you check the current selected tab in onResume() or onStart() of your Activity, and create/assign a new PagerAdapter for you ViewPager with the correct fragments?