I created a ViewPager and everything is working fine, however I want to have a previous next button outside of ViewPager which can be used to navigate inside ViewPager. How can I go to next Item on ViewPager without swiping manually?
Asked
Active
Viewed 6.7k times
116
-
40Doesn't this work viewpager.setCurrentItem(int index); ? – blessanm86 Oct 18 '11 at 03:58
-
it did , I suppose this question is a duplicate. – Vaibhav Mishra Oct 18 '11 at 04:15
-
1Possible duplicate of [How to change ViewPager's page?](http://stackoverflow.com/questions/7424562/how-to-change-viewpagers-page) – mmd1080 May 11 '17 at 04:08
4 Answers
168
As blessenm answered viewpager.setCurrentItem(int index)
is the way to go.

yprez
- 14,854
- 11
- 55
- 70

Vaibhav Mishra
- 11,384
- 12
- 45
- 58
-
7Unfortunately I do get different behavior, when the user swipes manually and when I jump using setCurrentItem. The order of calls is reversed. When I swipe, it first calls OnPageChangeListener#onPageSelected and then it calls setUserVisibleHint in the fragments. If I use setCurrentItem, it first calls setUserVisibleHint in the fragments and then it calls OnPageChangeListener#onPageSelected, which really is a problem in my case :/ so I was hoping to find a way to keep the natural behavior but still programmtically move to another page. – AgentKnopf Dec 06 '14 at 09:54
-
-
@AgentKnopf Did you manage to find a solution for this issue ? I also see this weird behavior but can't find a way to keep the flow of the regular swipe when I programmatically switch to the next/prev fragment. – Gilad Eshkoli Dec 27 '16 at 07:34
-
@Gil I am afraid not - i removed the programmatical changing of the current view and just allowed swiping. However we completely changed the implementation later so I didn't pursue this issue any further. – AgentKnopf Jan 02 '17 at 10:04
118
A complete implementation just for completeness:
public void MoveNext(View view) {
//it doesn't matter if you're already in the last item
pager.setCurrentItem(pager.getCurrentItem() + 1);
}
public void MovePrevious(View view) {
//it doesn't matter if you're already in the first item
pager.setCurrentItem(pager.getCurrentItem() - 1);
}

Androiderson
- 16,865
- 6
- 62
- 72
-
You might need it. For instance, if the change is triggered by a click, you could use `(nextBt.getId() == view.id)` to know what caused it – Tomas Wolf Oct 09 '14 at 16:32
-
2@Sagar he might have set the click listener of button in xml, in that case you must have the view parameter, even if you don't need it. – Ashish Tanna Jun 23 '15 at 23:24
-
19
Easiest way is:
nextButton.setOnClickListener { pager.arrowScroll(View.FOCUS_RIGHT) }
prevButton.setOnClickListener { pager.arrowScroll(View.FOCUS_LEFT) }

Pitel
- 5,334
- 7
- 45
- 72
0
i fix it Better, ty Androiderson.
private void MoveNextTopSlideShow(View view)
{
if (_viewPager_TopImageSlide.CurrentItem == _viewPager_TopImageSlide.ChildCount)
{
if (_viewPager_TopImageSlide.ChildCount > 0)
{
_viewPager_TopImageSlide.SetCurrentItem(0,true);
}
}
else
{
//it doesn't matter if you're already in the last item
_viewPager_TopImageSlide.SetCurrentItem(_viewPager_TopImageSlide.CurrentItem + 1, true);
}
}
private void MovePreviousTopSlideShow(View view)
{
if (_viewPager_TopImageSlide.CurrentItem == 0)
{
if (_viewPager_TopImageSlide.ChildCount > 0)
{
_viewPager_TopImageSlide.SetCurrentItem(_viewPager_TopImageSlide.ChildCount-1, true);
}
}
else
{
//it doesn't matter if you're already in the first item
_viewPager_TopImageSlide.SetCurrentItem(_viewPager_TopImageSlide.CurrentItem - 1, true);
}
}

Arman
- 47
- 2
- 8