Viewpager在滑动和选项卡上禁用最后一页

时间:2016-04-01 09:50:00

标签: android android-viewpager

我的目标是禁用标签和滑动到最后一页  当用户点击最后一页或尝试滑动到最后一页时,我想要回调,以便我可以显示祝酒..

目前我正在关注此但无法制作

public class CustomSwipeViewPager extends ViewPager {

private boolean mLastPageEnabled = false;
private int mLastPageIndex = 0;

public NoSwipeViewPager(Context context) {
    super(context);
}

public NoSwipeViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public void setLastPageEnabled(boolean enabled) {
    mLastPageEnabled = enabled;
}

public void setLastPageIndex(int index) {
    mLastPageIndex = index;
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event){

    if(!mLastPageEnabled && getCurrentItem() >= (mLastPageIndex - 1)) {
        // Always return false to disable user swipes
        return false;
    }

    return true;
}

@Override
public boolean onTouchEvent(MotionEvent event) {

    if (!mLastPageEnabled && getCurrentItem() >= (mLastPageIndex - 1)) {
        // Always return false to disable user swipes
        return false;
    }

    return true;
}

}

1 个答案:

答案 0 :(得分:1)

试试这个。希望这可以按你的意愿运作。

@Override
    public boolean onInterceptTouchEvent(MotionEvent event) {



        if (!mLastPageEnabled && getCurrentItem() > (mLastPageIndex - 1)) {

            switch (event.getAction()) {
                case (MotionEvent.ACTION_DOWN):
                    x1 = event.getX();
                    y1 = event.getY();
                    break;
                case (MotionEvent.ACTION_UP): {
                    x2 = event.getX();
                    y2 = event.getY();
                    dx = x2 - x1;
                    dy = y2 - y1;

                    // Use dx and dy to determine the direction
                    if (Math.abs(dx) > Math.abs(dy)) {
                        if (dx > 0) {
                            //right//previous
                            return super.onInterceptTouchEvent(event);
                        } else {
                            //left//next
                            return false;
                        }
                    }
                    else
                    {
                         return false;
                    }
                }
                break;
            }


        }

        return super.onInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (!mLastPageEnabled && getCurrentItem() > (mLastPageIndex - 1)) {

            switch (event.getAction()) {
                case (MotionEvent.ACTION_DOWN):
                    x1 = event.getX();
                    y1 = event.getY();
                    break;
                case (MotionEvent.ACTION_UP): {
                    x2 = event.getX();
                    y2 = event.getY();
                    dx = x2 - x1;
                    dy = y2 - y1;

                    // Use dx and dy to determine the direction
                    if (Math.abs(dx) > Math.abs(dy)) {
                        if (dx > 0) {
                            //right//previous
                            return super.onTouchEvent(event);
                        } else {
                            //left//next
                            return false;
                        }
                    }
                    else
                    {
                         return false;
                    }
                }
                break;
            }


        }

        return super.onTouchEvent(event);
    }