Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect a click in an onTouch listener?

I have a ViewPager inside a ScrollView. I need to be able to scroll horizontally as well as vertically. In order to achieve this had to disable the vertical scrolling whenever my ViewPager is touched (v.getParent().requestDisallowInterceptTouchEvent(true);), so that it can be scrolled horizontally.

But at the same time I need to be able to click the viewPager to open it in full screen mode.

The problem is that onTouch gets called before onClick and my OnClick is never called.

How can I implement both on touch an onClick?

viewPager.setOnTouchListener(new OnTouchListener() {     @Override     public boolean onTouch(View v, MotionEvent event) {         System.out.println("TOUCHED ");         if(event.getAction() == MotionEvent.???){             //open fullscreen activity         }         v.getParent().requestDisallowInterceptTouchEvent(true); //This cannot be removed         return false;     } });  viewPager.setOnClickListener(new OnClickListener() {     @Override     public void onClick(View v) {         System.out.println("CLICKED ");         Intent fullPhotoIntent = new Intent(context, FullPhotoActivity.class);         fullPhotoIntent.putStringArrayListExtra("imageUrls", imageUrls);         startActivity(fullPhotoIntent);     } }); 
like image 518
code511788465541441 Avatar asked Jul 24 '13 10:07

code511788465541441


2 Answers

Masoud Dadashi's answer helped me figure it out.

here is how it looks in the end.

viewPager.setOnTouchListener(new OnTouchListener() {     private int CLICK_ACTION_THRESHOLD = 200;     private float startX;     private float startY;      @Override     public boolean onTouch(View v, MotionEvent event) {         switch (event.getAction()) {         case MotionEvent.ACTION_DOWN:             startX = event.getX();             startY = event.getY();             break;         case MotionEvent.ACTION_UP:              float endX = event.getX();             float endY = event.getY();             if (isAClick(startX, endX, startY, endY)) {                  launchFullPhotoActivity(imageUrls);// WE HAVE A CLICK!!             }             break;         }         v.getParent().requestDisallowInterceptTouchEvent(true); //specific to my project         return false; //specific to my project     }      private boolean isAClick(float startX, float endX, float startY, float endY) {         float differenceX = Math.abs(startX - endX);         float differenceY = Math.abs(startY - endY);         return !(differenceX > CLICK_ACTION_THRESHOLD/* =5 */ || differenceY > CLICK_ACTION_THRESHOLD);     }  } 
like image 189
code511788465541441 Avatar answered Oct 01 '22 23:10

code511788465541441


I did something really simple by checking the time the user touches the screen.

private static int CLICK_THRESHOLD = 100;  @Override public boolean onTouch(View v, MotionEvent event) {     long duration = event.getEventTime() - event.getDownTime();      if (event.getAction() == MotionEvent.ACTION_UP && duration < CLICK_THRESHOLD) {         Log.w("bla", "you clicked!");     }      return false; } 

Also worth noting that GestureDetector has something like this built-in. Look at onSingleTapUp

like image 35
nilsi Avatar answered Oct 01 '22 22:10

nilsi