4

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.

Lampione
  • 1,622
  • 3
  • 21
  • 39

2 Answers2

0

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

ViewPager.setCurrentItem(3);
Mrugesh
  • 4,381
  • 8
  • 42
  • 84
  • As I said, this is not working for me. The ViewPager still shows the other fragments when animating. – Lampione May 16 '16 at 08:04
  • 1
    when viewpager is created, it gets predefined flow of fragments ,so it is not possible for viewpager to jump directly from A to D fragment but it will pass throgh middle fragments very fast – Mrugesh May 16 '16 at 08:10
  • I've accepted this answer as it seems there are no direct way to achieve what I'm looking for.. eventually I'll try to implement a custom adapter doing this in the future – Lampione May 29 '16 at 10:53
  • this is not right. This uses `smoothScroll true` under the hood (unless first layout), you need to use `setCurrentItem(index, false)` https://stackoverflow.com/questions/14684913/disabling-animation-in-viewpager – hmac Mar 25 '19 at 14:03
  • @hmac but then we are loosing animation completely, which is not what OP wants. – Starwave Oct 05 '21 at 12:44
0

I think, you should create PagerAdapter like bellow

public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;

public PagerAdapter(FragmentManager fm, int NumOfTabs) {
    super(fm);
    this.mNumOfTabs = NumOfTabs;
}

@Override
public Fragment getItem(int position) {

    Fragment fragment = null;
    switch (position) {
        case 0:
            fragment = new Fragment_PatientList();

            break;
        case 1:
            fragment = new FragmentB();
            break;
        case 2:
            fragment = new FragmentC();
            break;
        default:
            return null;
    }
    return fragment;
}

@Override
public int getCount() {
    return mNumOfTabs;
}

}

After creating pagerAdapter ,set viewPager.setAdapter(adapter) At the end, you can connect tablayout with your viewpager like this

 tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }
Fatih Altuntaş
  • 376
  • 2
  • 10