0

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!

KT79
  • 89
  • 1
  • 2
  • 10

2 Answers2

1

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

Ankur Aggarwal
  • 2,210
  • 2
  • 34
  • 39
1

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.

Community
  • 1
  • 1
Than
  • 2,759
  • 1
  • 20
  • 41
  • Thanks :) What's the best way to index all the pages (fragments) and then pick them up? I'm unsure how I give a page a set index and then use the setCurrent method to pick this up. Do I just give each page an index of say 1-25 and then use setCurrent with the argument of the page index I want to go to? – KT79 Oct 26 '15 at 15:25
  • It's hard to say without knowing what are you doing excatcly, but indexing pages from 0 to 24 seems reasonable (you probably keep that it in Collection anyway and retrieve if from that collection in adapter insantiateItem). You can write some helper function to help you search item Collection for desired item position. – Than Oct 26 '15 at 15:52