0

How do I build a ViewPager that detects both swipe and click events?

I tried creating a custom ViewPager class that overrides onInterceptTouchEvent to register both click and swipe events (because I know they interfere with each-other), and that's were I got stuck.

How to proceed?

public class CustomViewPager extends ViewPager {

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

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

    @Override
    public null onInterceptTouchEvent(MotionEvent ev) {
        //Enter the code here to communicate a touch event
        super.onInterceptTouchEvent(ev);

    }
}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
the_prole
  • 8,275
  • 16
  • 78
  • 163

2 Answers2

0

IF you are using a custom touch listener as in the example in the question, the trick to supporting clicks is using a Gesture Detector.

public class CustomViewPager extends ViewPager {

    private GestureDetector mGesture;

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

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

    private void init() {
        mGesture = new GestureDetector(getContext(), new GestureDetector.SimpleOnGestureListener() {

            @Override
            public boolean onSingleTapConfirmed(MotionEvent e) {
               //Handle your click here
                return true;
            }

        });
    }

    @Override
    public null onInterceptTouchEvent(MotionEvent ev) {
        if (!mGesture.onTouchEvent(event)) {
            super.onInterceptTouchEvent(ev);
        }
    }

}
Nick Cardoso
  • 20,807
  • 14
  • 73
  • 124
  • No you linked to *an* answer in your comments. That doesn't mean this approach won't work. If you want your link to be an answer, make an answer. People browsing this question later will look for an accepted answer, not comments – Nick Cardoso Dec 31 '15 at 00:01
  • If I could, I would have just flagged my question as a duplicate. I will make an answer. – the_prole Dec 31 '15 at 00:16
0

If you're not using a custom touch listener you can build your listener into the page content with the default listerner (as found here) within your Adapter.

something like this:

@Override
public Object instantiateItem(View collection, final int pos) { //have to make final so we can see it inside of onClick()

    LayoutInflater inflater = (LayoutInflater) collection.getContext()
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View page = inflater.inflate(R.layout.YOUR_PAGE, null);

    page.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            //this will log the page number that was click
            Log.i("TAG", "This page was clicked: " + pos);
        }
    });

    ((ViewPager) collection).addView(page, 0);

    return page;
}
Community
  • 1
  • 1
the_prole
  • 8,275
  • 16
  • 78
  • 163