问题描述
我已经在我的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); }
问题描述
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); }
相关问答
相关标签/搜索