查看页面的导航按钮[英] ViewPager navigation buttons

本文是小编为大家收集整理的关于查看页面的导航按钮的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

我已经在我的XML-Pager文件中有两个按钮"左"和"右". 我想设置它们:当我点击"左"按钮时,页面自动向左滑动,当我点击"右"按钮时,它会自动向右移动.有人可以给我一个代码放在我的活动文件中吗?谢谢!

推荐答案

要去下一个/上一页使用以下内容:

private void nextPage() {
    int currentPage = viewPager.getCurrent();
    int totalPages = viewPager.getAdapter().getCount();

    int nextPage = currentPage+1;
    if (nextPage >= totalPages) {
        // We can't go forward anymore.
        // Loop to the first page. If you don't want looping just
        // return here.
        nextPage = 0;
    }

    viewPager.setCurrentItem(nextPage, true);
}

private void previousPage() {
    int currentPage = viewPager.getCurrent();
    int totalPages = viewPager.getAdapter().getCount();

    int previousPage = currentPage-1;
    if (previousPage < 0) {
        // We can't go back anymore.
        // Loop to the last page. If you don't want looping just
        // return here.
        previousPage = totalPages - 1;
    }

    viewPager.setCurrentItem(previousPage, true);
}

本文地址:https://www.itbaoku.cn/post/102601.html

问题描述

I already have the two buttons "left" and "right" in my xml-pager file.. I want to set them: when i click on "left" button the page swipe automaticlly to left and when i click on "right" button it goes automatically to right.. can someone give me the code to put in my activity file? thank you!

推荐答案

To go the next/previous page use something like the following:

private void nextPage() {
    int currentPage = viewPager.getCurrent();
    int totalPages = viewPager.getAdapter().getCount();

    int nextPage = currentPage+1;
    if (nextPage >= totalPages) {
        // We can't go forward anymore.
        // Loop to the first page. If you don't want looping just
        // return here.
        nextPage = 0;
    }

    viewPager.setCurrentItem(nextPage, true);
}

private void previousPage() {
    int currentPage = viewPager.getCurrent();
    int totalPages = viewPager.getAdapter().getCount();

    int previousPage = currentPage-1;
    if (previousPage < 0) {
        // We can't go back anymore.
        // Loop to the last page. If you don't want looping just
        // return here.
        previousPage = totalPages - 1;
    }

    viewPager.setCurrentItem(previousPage, true);
}