0

I am trying to implement a sliding tab layout that and each fragment should only load when the tab is selected instead i get the adjacent fragment loading, I have tried setOffScreenPageLimit(1) as my code shows but still the adjacent fragment gets loaded any assistance please ???

private SlidingTabLayout tabLayout;
private ViewPager viewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);       
    viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
    viewPager.setOffscreenPageLimit(1);
    tabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
    if (tabLayout != null) {
        tabLayout.setViewPager(viewPager);
        tabLayout.setDistributeEvenly(true);
        tabLayout.setSelectedIndicatorColors(R.color.md_amber_600);
    }
Rahul Patil
  • 2,707
  • 2
  • 21
  • 32
Andy Cass
  • 398
  • 6
  • 16
  • as per [this link](http://stackoverflow.com/questions/19096868/how-can-make-my-viewpager-load-only-one-page-at-a-time-ie-setoffscreenpagelimit), viewpager has to load 2 pages at a time to have sliding better way – Ichigo Kurosaki Sep 06 '16 at 09:02

1 Answers1

0

I can see you have used setOffScreenPageLimit as 1. It means you are loading only 1 extra screen at a time using viewPager, when actually you want to load nothing. It is not possible to set 0 value for setOffScreenPageLimit this method, actually you can set it but it will not work.

So, it is not possible to achieve this using view pager. If you still want to perform such a functionality, you can use FrameLayout and switch between the fragments based on tab selection.

Below is the tab selection code sample:

tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            switchToTab(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });