OnPageScrolled在Android上不起作用

时间:2013-08-16 10:44:35

标签: android android-viewpager

我知道StackOverflow上有一些类似的问题,但这些解决方案都不适用于我。我想使用方法onPageScrolled,我已经设置了listner,适配器等,但它仍然没有反应。 这是我的代码: 我通过以下方式实现此方法:

public class ChefViewActivity extends RoboFragmentActivity implements  ViewPager.OnPageChangeListener {

然后:

....
pagerIndicator = new PagerIndicator((LinearLayout) findViewById(R.id.pagerIndicator),
            fragments.size());
    fragmentAdapter = new DetailsFragmentAdapter(getSupportFragmentManager(), fragments,
            pagerIndicator);

    viewPager = (ViewPager) findViewById(R.id.pager);
    viewPager.setAdapter(fragmentAdapter);
    viewPager.setOnPageChangeListener(pagerIndicator);
    viewPager.setOffscreenPageLimit(5);
    pagerIndicator.onPageSelected(viewPager.getCurrentItem());
.....

2 个答案:

答案 0 :(得分:2)

以下代码适合我。

viewPager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {

            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });

答案 1 :(得分:0)

private class GestureListener extends SimpleOnGestureListener {

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float    velocityY) {


if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) >   SWIPE_THRESHOLD_VELOCITY) {
            return false; // Right to left
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX)    > SWIPE_THRESHOLD_VELOCITY) {
            return false; // Left to right
        }

        if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) >  SWIPE_THRESHOLD_VELOCITY) {
            return false; // Bottom to top
        }  else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
            return false; // Top to bottom
        }
        return false;
    }
}