How to programmatically show next view in ViewPager?
Asked Answered
R

5

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?

Rite answered 18/10, 2011 at 3:3 Comment(3)
Doesn't this work viewpager.setCurrentItem(int index); ?Salaidh
it did , I suppose this question is a duplicate.Rite
Possible duplicate of How to change ViewPager's page?Meneses
R
169

As blessenm answered viewpager.setCurrentItem(int index) is the way to go.

Rite answered 12/11, 2011 at 9:26 Comment(4)
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.Hellman
@Vaibhav what if I want to show the next view of activityBank
@Hellman 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.Brittney
@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.Hellman
M
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);
}
Mourant answered 16/8, 2013 at 17:40 Comment(3)
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 itGathers
@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.Chiarra
page swiping animation not coming?Disquisition
H
19

Easiest way is:

nextButton.setOnClickListener { pager.arrowScroll(View.FOCUS_RIGHT) }
prevButton.setOnClickListener { pager.arrowScroll(View.FOCUS_LEFT) }
Hypsometry answered 11/10, 2018 at 9:10 Comment(0)
B
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);
        }
    }
Bizarre answered 21/4, 2017 at 6:10 Comment(0)
C
0

viewPager.setCurrentItem(currentStep, true), where the second parameter it's boolean smoothScroll.

Callery answered 20/12, 2023 at 21:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.