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?
How to programmatically show next view in ViewPager?
Asked Answered
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
As blessenm answered viewpager.setCurrentItem(int index)
is the way to go.
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 activity –
Bank
@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
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);
}
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 –
Gathers @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
Easiest way is:
nextButton.setOnClickListener { pager.arrowScroll(View.FOCUS_RIGHT) }
prevButton.setOnClickListener { pager.arrowScroll(View.FOCUS_LEFT) }
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);
}
}
viewPager.setCurrentItem(currentStep, true)
,
where the second parameter it's boolean smoothScroll.
© 2022 - 2024 — McMap. All rights reserved.