Android viewpager检测滑动超出范围

Android viewpager detect swipe beyond the bounds(Android viewpager检测滑动超出范围)

本文介绍了Android viewpager检测滑动超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的 Android 应用程序中,我使用 viewpager 进行图像滑动.我的要求是,如果用户滑出第一页和最后一页,则活动应该完成.

In my Android application, I am using a viewpager for image swiping. My requirement is, if a user swipes out of the first and last page, the activity should finish.

我采用了这个示例.但是方法 setOnSwipeOutListener 在我的活动中没有被调用.

I have taken this example. But the method setOnSwipeOutListener is not called in my activity.

这是我的自定义视图寻呼机类:

This is my custom view pager class:

public class CustomViewPager extends ViewPager {

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

    public CustomViewPager(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();
    }

} 

在我的活动类下面,我在自定义 viewpager 类上调用 setOnSwipeOutListener 方法,但它没有被调用.

And below in my activity class I call setOnSwipeOutListener method on my custom viewpager class, but it does not get called.

myPager = (CustomViewPager) findViewById(R.id.home_pannels_pager);

.......

myPager.setOnSwipeOutListener(new OnSwipeOutListener() { // the method never called
    @Override
    public void onSwipeOutAtStart() {
        Log.e("swipe Out At Start ", "swipe out");
    }

    @Override
    public void onSwipeOutAtEnd() {
        Log.e("swipe Out At End ", "swipe end");
    }
});

myPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
    @Override
    public void onPageSelected(int po) {
        Log.e("positon", ""+po);
    }
});

请帮助我检测用户是否滑动到第一页和最后一页以关闭此活动以及如何从代码中调用该方法.

Please help me to detect if the user has swiped beyond the first and last page to close this activity and how to call the method from code.

推荐答案

就我而言,onInterceptTouchEvent 不起作用.我用 onTouchEvent 改变了方法,它正在工作.原始解决方案来自 tjlian616

In my case, onInterceptTouchEvent does not work. I change the method with onTouchEvent and it is working. The original solution is from tjlian616

    @Override
public boolean onTouchEvent(MotionEvent ev){
    if(getCurrentItem()==getAdapter().getCount()-1){
        final int action = ev.getAction();
        float x = ev.getX();
        switch(action & MotionEventCompat.ACTION_MASK){
        case MotionEvent.ACTION_DOWN:
            mStartDragX = x;
            break;
        case MotionEvent.ACTION_MOVE:
            break;
        case MotionEvent.ACTION_UP:
            if (x<mStartDragX){
                mListener.onSwipeOutAtEnd();
            }else{
                mStartDragX = 0;
            }
            break;
        }
    }else{
        mStartDragX=0;
    }
    return super.onTouchEvent(ev);
}    

这篇关于Android viewpager检测滑动超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Android viewpager检测滑动超出范围

基础教程推荐