0

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

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());
.....
4

2 回答 2

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) {

            }
        });
于 2013-08-16T10:49:18.023 回答
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;
    }
}
于 2013-08-16T10:50:40.517 回答