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.

2 Answers 2

0

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

ViewPager.setCurrentItem(3); 
Sign up to request clarification or add additional context in comments.

5 Comments

As I said, this is not working for me. The ViewPager still shows the other fragments when animating.
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
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
this is not right. This uses smoothScroll true under the hood (unless first layout), you need to use setCurrentItem(index, false) stackoverflow.com/questions/14684913/…
@hmac but then we are loosing animation completely, which is not what OP wants.
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()); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.