0

这是我在 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 不可滚动,所以我只能从按钮更改页面?

4

1 回答 1

0

像这样创建了一个自定义 ViewPager:

public class CustomViewPager extends ViewPager {

private boolean isPagingEnabled;

public CustomViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.isPagingEnabled = true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.isPagingEnabled) {
        return super.onTouchEvent(event);
    }

    return false;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    if (this.isPagingEnabled) {
        return super.onInterceptTouchEvent(event);
    }

    return false;
}

public void setPagingEnabled(boolean b) {
    this.isPagingEnabled = b;
}
}
于 2013-02-18T12:59:31.280 回答