且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

检测用户何时从页面边缘滑动Viewpager

更新时间:2023-12-01 20:53:46

我认为,您应该重写ViewPager容器的几种方法.例如,如果是LinearLayout

I think, you should override few methods of the container of your ViewPager. For example if it is LinearLayout

public class Liner extends LinearLayout {

public Liner(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    // TODO Auto-generated constructor stub
}

//Remember start points
float mInitX;
float mInitY;
// TouchSlop value
float mSomeValue=20F;
// 
boolean mCatched=false;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {

    switch (ev.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mInitX=ev.getX();
        mInitY=ev.getY();
        break;

    default:
        break;
    }

    mCatched=mInitX<10 && Math.abs(ev.getX()-mInitX)>mSomeValue;

    return mCatched;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if(mCatched){
        //Do all you want here
    }
    return true;
}

}

如果您与 TeRRo 结合使用(将其检测器放在onTouchEvent()内),您可能会得到不错的结果.但是使用DrawerLayout的***方法.

If you combine with TeRRo anrwer (put his detector inside onTouchEvent()) you may have good result. But best way to use DrawerLayout.

祝你好运!