如何以编程方式在 ViewPager 中显示下一个视图?

发布于 2024-12-10 04:41:44 字数 114 浏览 1 评论 0原文

我创建了一个 ViewPager,一切工作正常,但是我希望在 ViewPager 之外有一个上一个下一个按钮,可用于在 ViewPager 内导航。如何在不手动滑动的情况下转到 ViewPager 上的下一个项目?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

放我走吧 2024-12-17 04:41:44

正如 blessenm 回答的那样, viewpager.setCurrentItem(int index) 是可行的方法。

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

稀香 2024-12-17 04:41:44

一个完整的实现只是为了完整性:

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);
}

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);
}
清君侧 2024-12-17 04:41:44

最简单的方法是:

nextButton.setOnClickListener { pager.arrowScroll(View.FOCUS_RIGHT) }
prevButton.setOnClickListener { pager.arrowScroll(View.FOCUS_LEFT) }

Easiest way is:

nextButton.setOnClickListener { pager.arrowScroll(View.FOCUS_RIGHT) }
prevButton.setOnClickListener { pager.arrowScroll(View.FOCUS_LEFT) }
人事已非 2024-12-17 04:41:44

我把它修得更好了,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);
        }
    }

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);
        }
    }
栩栩如生 2024-12-17 04:41:44

viewPager.setCurrentItem(currentStep, true),
其中第二个参数是布尔值 smoothScroll。

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文