1

I want to let the ListView intercept all the events, except the SingleTap.

First I did the code bellow

  private float startX;
    private float startY;

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
         if (event.getAction() == MotionEvent.ACTION_DOWN ) {
             Log.d("intercept", "down");
            startX = event.getX();
            startY = event.getY();


        }
        if (event.getAction() == MotionEvent.ACTION_UP) {
             Log.d("intercept", "up");
            float deltaX = Math.abs(event.getX() - startX);
            float deltaY = Math.abs(event.getY() - startY);
            if(deltaX<5 && deltaY<5){

                return false;
            }

        }

       return super.onInterceptTouchEvent(event);
    }

With that I was able to see that I really detect the singleTap (intercept-down and intercep-up on LogCat), but it block my list to handle other moves. After that I change return super.onInterceptTouchEvent(event); to return true and it works for the rest of the moves. The problem is that I was not able anymor to detect the singleTap now. I can see that intercept-down on Logcat, but not intercept-up. I already tried this another code withou sucess too.

import android.content.Context;
import android.support.v4.view.GestureDetectorCompat;
import android.util.AttributeSet;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.ListView;


public class SwipeListView extends ListView  {
    Context context;
    private GestureDetectorCompat mDetector;
    public SwipeListView(Context context) {
        super(context);
    }

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

    public SwipeListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    public void setContext(Context context){
        this.context= context;
        mDetector = new GestureDetectorCompat(context, new MyGestureListener());
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return !onTouchEvent(event);
    }
    @Override
    public boolean onTouchEvent(MotionEvent e) {
        return mDetector.onTouchEvent(e);
    }

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        private static final String DEBUG_TAG = "Gestures";

        @Override
        public boolean onDown(MotionEvent event) {
            Log.e(DEBUG_TAG,"onDown: " + event.toString());
            return true;
        }

        @Override
        public boolean onSingleTapConfirmed(MotionEvent event) {
            Log.e(DEBUG_TAG, "onSingleTap: " + event.toString());
            return true;
        }
    }
}

Anyone could help me to detect the singleTap correctly?

user3249186
  • 185
  • 4
  • 13
  • http://stackoverflow.com/questions/13324250/android-two-different-events-for-single-tap-and-long-press-double-tap – Pararth Feb 04 '14 at 04:51
  • I tried. It didn`t work.In fact, it block all the clicks. Could you look the code again and help me to find what I am doing wrong? I edited and posted all the class. – user3249186 Feb 04 '14 at 06:21
  • I think I figured out what's going on, but do not know to solve. MyGestureListener is only capturing Down. The log shows just a sequence of Downs, but zero SingleTapConfirmed. – user3249186 Feb 04 '14 at 06:29
  • call mDetector.onTouchEvent in dispatchTouchEvent, make sure to call super.dispatchTouchEvent – pskink Feb 04 '14 at 06:35
  • Didnt work. If I call just dispatchTouchEvent the way you said only the SingleTap work, but the list dont intercept the others events – user3249186 Feb 04 '14 at 06:47
  • @user3249186 override only dispatchTouchEvent, remove other methods – pskink Feb 04 '14 at 06:55
  • The problem is that I need to onInterceptTouchEvent(MotionEvent event) return false just when the event is a SingleTap, but the method return false as soon as onDown return true and it block the UI for clicks because there is not a complete event to handle. – user3249186 Feb 04 '14 at 06:56
  • @user3249186 you said you want to intercept all ListView touch events: dispatchTouchEvent is the right place you are looking for – pskink Feb 04 '14 at 07:07
  • I removed and override that way. @Override public boolean dispatchTouchEvent(MotionEvent ev) { this.mDetector.onTouchEvent(ev); return super.dispatchTouchEvent(ev); } – user3249186 Feb 04 '14 at 07:10
  • @user3249186 exactly what i meant – pskink Feb 04 '14 at 07:12
  • @pskink I dont want to intercept all of them. I want to let the child handle the SingleTap. With dispatchTouchEvent it let the child handle the SingleTap but it dind`t let the parent handle the swipe. – user3249186 Feb 04 '14 at 16:29
  • swipe? what swipe? you have not mention about any swipes before. what exactly do you want to achieve? – pskink Feb 04 '14 at 16:51
  • I am working with a library to implement SwipeToDismiss function. I want to start a Intent when I click on a element of the list too. But when I setOnClickListener on the elements of the list it consume the event, and I am not able to get swipe to Dismiss anymore. – user3249186 Feb 04 '14 at 18:46

1 Answers1

0

Trying it with your code, meanwhile,
Did you try: Return true from touch down/up with:

public boolean onTouchEvent(MotionEvent e) {
    return gestureScanner.onTouchEvent(e);
}

public boolean onSingleTapConfirmed(MotionEvent e) { 

   //this works for me

    return true; 
}

public boolean onDown(MotionEvent e) { return true; }


public void onLongPress(MotionEvent e) {
    //

}
Pararth
  • 8,114
  • 4
  • 34
  • 51
  • I edit the code with this try. I think I know what is the problem. Seems like it is handle with Down like it is a complete MotionEvent. It doesnt wait until SingleTap complete to return true. – user3249186 Feb 04 '14 at 06:51