我的 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/