如何显示警报如果ViewPager滚动到最后一页之外

时间:2016-08-23 07:16:50

标签: android android-layout android-studio android-viewpager viewpagerindicator

我有一个包含3个片段的viewpager。如果我在视图寻呼机中滚动到最后一页以外,我需要显示一个祝酒词或警告“没有更多页面可用”。

我已经尝试了很多,但没有任何帮助我。

任何帮助都应该非常感激。

我已添加了代码段,请仔细阅读

ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
      boolean callHappened;
      int selectedIndex;
      boolean mPageEnd;
    @Override
    public void onPageSelected(int position) {
        selectedIndex = position;
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
        // TODO Auto-generated method stub

        if( mPageEnd && arg0 == myViewPagerAdapter.getCount()-1 && !callHappened){
            Log.d(getClass().getName(), "Okay");
            mPageEnd = false;//To avoid multiple calls.
            callHappened = true;
        }else{
            mPageEnd = false;
        }
    }
    @Override
    public void onPageScrollStateChanged(int arg0) {
        // TODO Auto-generated method stub
        if(selectedIndex == myViewPagerAdapter.getCount() - 1)
        {
            mPageEnd = true;
        }
    }
};

4 个答案:

答案 0 :(得分:1)

您应该尝试为视图寻呼机制作自定义视图寻呼机类

    public class CustomViewPagerEndSwipe extends ViewPager {

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

    float mStartDragX;
    OnSwipeOutListener mListener;


    public void setOnSwipeOutListener(OnSwipeOutListener listener) {
        mListener = listener;
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        float x = ev.getX();
        switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mStartDragX = x;
            break;
        case MotionEvent.ACTION_MOVE:
            if (mStartDragX < x && getCurrentItem() == 0) {
                mListener.onSwipeOutAtStart();
            } else if (mStartDragX > x && getCurrentItem() == getAdapter().getCount() - 1) {
                mListener.onSwipeOutAtEnd();
            }
            break;
        }
        return super.onInterceptTouchEvent(ev);
    }

    public interface OnSwipeOutListener {
        public void onSwipeOutAtStart();
        public void onSwipeOutAtEnd();
    }
}

在布局xml中使用此类

<com.utils.customview.CustomViewPagerEndSwipe
            android:id="@+id/viewPager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            />

从您的活动和片段中实现侦听器,您将从界面

获取最后一页toast
viewPager.setOnSwipeOutListener(this);

答案 1 :(得分:1)

使用来自原始代码的一些代码和来自@Mohit Suthar的代码,但是,如果用户尝试在第一页之前滑动,则@Mohit Suthar的答案将显示许多警报(原因是因为它确实如此不使用提问者的代码&#34; callHappened&#34;变量以避免多次通话)

如果用户在片段的上部区域向左滑动,则以下代码不能提供完美的工作。等待修改

我建议使用enum进行滑动操作,因为如果你有另一个自定义逻辑,它会很灵活。

public class CustomViewPagerEndSwipe extends ViewPager {
    public CustomViewPagerEndSwipe(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    float mStartDragX;
    SwipeAction mSwipeAction;
    OnSwipeOutListener mListener;
    boolean callHappened = false;
    public void setOnSwipeOutListener(OnSwipeOutListener listener) {
        mListener = listener;
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        float x = ev.getX();
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                mStartDragX = x;
                callHappened = false;
                break;
            case MotionEvent.ACTION_MOVE:
                if (!callHappened) {
                    if (mStartDragX < x && getCurrentItem() == 0) {
                        mSwipeAction = SwipeAction.BEFORE_START_PAGE;
                        mListener.onSwipe(mSwipeAction);
                        callHappened = true;
                    } else if (mStartDragX > x && getCurrentItem() == getAdapter().getCount() - 1) {
                        mSwipeAction = SwipeAction.AFTER_LAST_PAGE;
                        mListener.onSwipe(mSwipeAction);
                        callHappened = true;
                    }
                }
                break;
        }
        return super.onInterceptTouchEvent(ev);
    }
    public enum SwipeAction {
        BEFORE_START_PAGE, AFTER_LAST_PAGE
    }
    public interface OnSwipeOutListener {
        void onSwipe(SwipeAction pSwipeAction);
    }
}

您在活动/片段中调用的代码

viewPager.setOnSwipeOutListener(new CustomViewPagerEndSwipe.OnSwipeOutListener() {
    @Override
    public void onSwipe(CustomViewPagerEndSwipe.SwipeAction pSwipeAction) {
        switch (pSwipeAction) {
            case BEFORE_START_PAGE:
                new AlertDialog.Builder(getActivity()).setMessage("Swipe out start").create().show();
                break;
            case AFTER_LAST_PAGE:
                new AlertDialog.Builder(getActivity()).setMessage("Swipe out end").create().show();
                break;
        }
    }
});

答案 2 :(得分:0)

如果返回false,您可以使用viewpager.canScrollHorizontally(View.SCROLL_AXIS_HORIZONTAL)方法进行检查,然后您就位于查看寻呼机的最后一项。

答案 3 :(得分:0)

更新自 @Mohit Suthar 的回答。在我的情况下,我使用吐司,我希望不那么敏感,并希望少发生。

private final static long TIME_MS = 2000;
private final static long THRESHOLD = 50;

OnSwipeOutListener mListener;
float mStartDragX;

private boolean activated = false;
private Timer mTimer;
private TimerTask mTimerTask;

@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
    float x = ev.getX();
    switch (ev.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mStartDragX = x;
            break;
        case MotionEvent.ACTION_MOVE:
            Log.JSC("TEST", "1 : " + (mStartDragX < x - 20 && getCurrentItem() == 0));
            Log.JSC("TEST", "2 : " + (activated));
            Log.JSC("TEST", "3 : " + mStartDragX);
            Log.JSC("TEST", "4 : " + x);
            Log.JSC("TEST", "5 : " + (x - 20));

            if (mStartDragX < x - THRESHOLD && getCurrentItem() == 0) {
                if (!activated) {
                    activated = true;
                    prepareTimer();
                    mTimer.schedule(mTimerTask, TIME_MS);
                    if (mListener != null) {
                        mListener.onSwipeOutAtStart();
                    }
                }
            } else if (mStartDragX - THRESHOLD > x && getCurrentItem() == getAdapter().getCount() - 1) {
                if (!activated) {
                    activated = true;
                    prepareTimer();
                    mTimer.schedule(mTimerTask, TIME_MS);
                    if (mListener != null) {
                        mListener.onSwipeOutAtEnd();
                    }
                }
            }
            break;
    }
    try {
        return super.onInterceptTouchEvent(ev);
    } catch (IllegalArgumentException e) {
        return false;
    }
}

private void prepareTimer() {
    mTimer = new Timer();
    mTimerTask = new TimerTask() {
        @Override
        public void run() {
            activated = false;
        }
    };
}