How to programmatically show next view in ViewPager?

前端 未结 4 1386

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

相关标签:
4条回答
  • 2021-02-01 00:46

    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);
    }
    
    0 讨论(0)
  • 2021-02-01 00:52

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

    0 讨论(0)
  • 2021-02-01 00:52

    Easiest way is:

    nextButton.setOnClickListener { pager.arrowScroll(View.FOCUS_RIGHT) }
    prevButton.setOnClickListener { pager.arrowScroll(View.FOCUS_LEFT) }
    
    0 讨论(0)
  • 2021-02-01 00:52

    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);
            }
        }
    
    0 讨论(0)
提交回复
热议问题