android - ViewPager 导航按钮

标签 android button swipe pager

我的 xml-pager 文件中已经有了“左”和“右”两个按钮。 我想设置它们:当我点击“左”按钮时,页面会自动向左滑动,当我点击“右”按钮时,它会自动向右滑动。有人可以给我代码以放入我的 Activity 文件吗?谢谢!

最佳答案

要转到下一页/上一页,请使用如下内容:

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

关于android - ViewPager 导航按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14305150/

相关文章:

forms - 按钮 Onclick 在表单内部不起作用

swipe - 使用 Hammer.js 识别对角线手势(滑动)

jquery - 水平滚动 - 移动 - 滑动?

android - 在 onBindViewHolder RecyclerView 中添加滑动

android - 使用 Parse 仅在本地存储数据

android - 使用 PendingIntent 获取位置始终返回 Null

android - view.getId() 在 OnItemClickListener 中返回错误的 id

java - 单击事件后从资源布局中 inflatedView

button - JavaFX TabPane 宽度和按钮宽度不匹配

apache-flex - 扩展 Flex Spark Button 以包含可单击的复选框