116

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?

Jonik
  • 80,077
  • 70
  • 264
  • 372
Vaibhav Mishra
  • 11,384
  • 12
  • 45
  • 58

4 Answers4

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
  • 7
    Unfortunately 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
  • @Vaibhav what if I want to show the next view of activity – Prabs Nov 17 '15 at 12:11
  • @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
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