I am implementing Pager Adapter in a Fragment
. When I load the screen First time, it works fine. If i switch to other fragment and goes to previous fragment again the it shows empty screen. If I swipe between different tap and move to first tab again then it shows data.
I think on moving back the tabs which are visible didn't load data but once they are out of view during swipe navigation it loads data.
Here is my Pager Adapter:
public class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
return new Fragment1();
case 1:
return new Fragment2();
case 2:
return new Fragment3();
case 3:
return new Fragment4();
case 4:
return new Fragment5();
case 5:
return new Fragment6();
}
return null;
}
@Override
public int getCount() {
return 6;
}
}
I am setting my adapter like this:
viewPager = (ViewPager) rootView.findViewById(R.id.my_pager);
mAdapter = new MyPagerAdapter(getActivity().getSupportFragmentManager());
viewPager.setAdapter(mAdapter);
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
I was having the same problem and wasted a whole day on this. So I am posting the solution for my issue so that someone else doesn't have to struggle and waste a lot of time.
My problem was that the Fragments inside viewpager
were getting invoked(debugger was getting hit) but I was not able to see it in the view(Even for the 1st time).
Issues were:
I had to use getChildFragmentManager()
instead of getFragmentManager()
.
For some reason, even if we keep the height and width of viewpager
as matchparent
, it was not getting picked up and was defaulted to 0(Even though it was filled in the preview of the xml).
To fix this, we have to add android:fillViewport="true"
in your NestedScrollView
Hopefully someone will find this helpfull :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With