KT79
KT79

Reputation: 89

Skipping to a specific page in a ViewPager from a Navigation Drawer

I'm a little confused with what I should be doing here so was hoping someone could point me in the right direction.

I have a Viewpager that has around 25 pages to it, I can swipe left and right through it fine. I also have a navigation drawer that I want to use to jump to a specific page within the 25 pages. I.e. say Page 1, 5 ,15 and 25.

I've been having a look at the setCurrentItem() and getCurrentItem() methods but not really sure how to implement them or if that's what I should be using.

My idea was to use the setCurrenItem() within the onNavigationDrawerItemSelected() method via a switch statement. Would that work? If so, I presume I'd need to give each of my fragments an int argument to specify each page and then use that from setCurrentItem()?

Any advice would be appreciated!

Upvotes: 0

Views: 622

Answers (2)

Than
Than

Reputation: 2769

ViewPager setCurrentItem method is not abstract, you don't have to implement it. Just call it from your activity/fragment/etc whenever you detect click on NavigationDrawer menu item. However you may want to have scrolling animation. In this case you need to provide your custom scroller to achieve smooth scroll effect:

ViewPager setCurrentItem(pageId, true) does NOT smoothscroll

Speaking of page selection you should know which fragment fragment is on which pager page, so that shouldn't be a problem as well.

Upvotes: 1

Ankur Aggarwal
Ankur Aggarwal

Reputation: 2220

Yes, @KT79, that is in fact the only way to have a "jump" effect in a ViewPager.

Upvotes: 1

Related Questions