这是我在 viewPager 上的监听器:
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
@Override
public void onPageSelected(int arg0) {
pageNr.setText("page: " + (viewPager.getCurrentItem() + 1) + "/" + viewPager.getChildCount());
currentSlide = viewPager.getCurrentItem();
page = viewPager.getCurrentItem();
viewPager.setAdapter(new SubcategoriesPagerAdapter(getActivity(), inflater, videos, numberOfPages));
}
@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
@Override
public void onPageScrollStateChanged(int arg0) {
}
});
这是我的按钮:
nextBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
goToNextSlide();
pageNr.setText("page: " + (viewPager.getCurrentItem() + 1) + "/" + viewPager.getChildCount());
if (categ == 2) {
numberOfPages = 2;
}
viewPager.setAdapter(new SubcategoriesPagerAdapter(getActivity(), inflater, videos, numberOfPages, categ));
}
});
previousBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
goToPreviousSlide();
pageNr.setText("page: " + (viewPager.getCurrentItem() + 1) + "/" + viewPager.getChildCount());
if (categ == 2) {
numberOfPages = 2;
}
viewPager.setAdapter(new SubcategoriesPagerAdapter(getActivity(), inflater, videos, numberOfPages, categ));
}
});
private void goToNextSlide() {
currentSlide = viewPager.getCurrentItem();
page = viewPager.getCurrentItem();
if (currentSlide < viewPager.getChildCount()) {
viewPager.setCurrentItem(currentSlide + 1);
}
}
private void goToPreviousSlide() {
currentSlide = viewPager.getCurrentItem();
page = viewPager.getCurrentItem();
if (currentSlide > 0) {
viewPager.setCurrentItem(currentSlide - 1);
}
}
现在,如果我按下下一个或上一个按钮,它会更改页面以及页面中的图片(我有 9 张图片,一个 3x3 的图像视图矩阵)当我滚动到第二页时,有时它会更改图片,有时它没有,而且viewpager也开始变得疯狂,有时我不能从第二页回到第一页,但我可以向右滚动,无限次,但每次它显示第二页。我能做什么。或者是否可以使 Viewpage 不可滚动,所以我只能从按钮更改页面?