Search code examples
androidandroid-fragmentsandroid-viewpagerandroid-tablayout

Skip pages in ViewPager + TabLayout


I know this question has already been asked, more than once, but I think I'm looking for a different kind of "skip".

In a ViewPager linked with a TabLayout:

ViewPager vp = (ViewPager) findViewById(...);
TabLayout tl = (TabLayout) findViewById(...);
vp.setAdapter(new NoSwipePagerAdapter());
tl.setupWithViewPager(vp);

Since the viewpager is not scrollable, the expected behavior would be like: if tap on the "A" tab, viewpager scrolls to that tab.

My problem is that if I'm on "A" and I tap "D", the viewpager animate through B and C. What I'm trying to do is to literally "skip" B and C and show D.

Mainly for a matter of performance, since B and C are heavy fragments, but also for a matter of design.

I've tried every combination of setCurrentItem(int index, boolean smoothScroll), and it didn't work.

Is it possible to skip ViewPager pages? Thanks in advance.


Solution

  • if you want to skip two fragments and directly go to fourth fragment then write the following line

    ViewPager.setCurrentItem(3);