Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The GestureDetector does not work (example from android developer)

I am reading the gesture in android at android developer, and following the tutorial I tried to run the following codes:

public class MainActivity extends Activity implements OnGestureListener, OnDoubleTapListener {

    private static final String DEBUG_TAG = "Gestures";
    private GestureDetectorCompat mDetector;

    // Called when the activity is first created.
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        View v = new RelativeLayout(this);
        v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
        setContentView(v);
        mDetector = new GestureDetectorCompat(this, this);
        mDetector.setOnDoubleTapListener(this);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        this.mDetector.onTouchEvent(event);
        return super.onTouchEvent(event);
    }

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

    @Override
    public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
        Log.d(DEBUG_TAG, "onFling: " + event1.toString() + event2.toString());
        return true;
    }

    @Override
    public void onLongPress(MotionEvent event) {
        Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
    }

    @Override
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
        Log.d(DEBUG_TAG, "onScroll: " + e1.toString() + e2.toString());
        return true;
    }

    @Override
    public void onShowPress(MotionEvent event) {
        Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
    }

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

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

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

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

The codes came from the site here.

But when I ran the app, I got no debug information when I click or long press the view.

BTW, I test the app in both emulator and htc e1 device.

What is the problem?

like image 982
hguser Avatar asked May 09 '13 00:05

hguser


People also ask

How do you use GestureDetector?

Detect gestures. Android provides the GestureDetector class for detecting common gestures. Some of the gestures it supports include onDown() , onLongPress() , onFling() , and so on. You can use GestureDetector in conjunction with the onTouchEvent() method described above.

What does the GestureDetector OnGestureListener interface do when implemented?

OnGestureListener. The listener that is used to notify when gestures occur. If you want to listen for all the different gestures then implement this interface.

What is touch gestures in Android?

Android App Development for Beginners Android provides special types of touch screen events such as pinch , double tap, scrolls , long presses and flinch. These are all known as gestures. Android provides GestureDetector class to receive motion events and tell us that these events correspond to gestures or not.


2 Answers

You are creating a GestureDetector but you are never "hooking it up" to your View. Try changing your onCreate like this:

super.onCreate(savedInstanceState);
View v = new RelativeLayout(this);
v.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
setContentView(v);
mDetector = new GestureDetectorCompat(this, this);
mDetector.setOnDoubleTapListener(this);
v.setOnTouchListener(new OnTouchListener(){
    public boolean onTouch(View v, MotionEvent me){
        return mDetector.onTouchEvent(me);
    }
});
like image 62
FoamyGuy Avatar answered Nov 15 '22 15:11

FoamyGuy


Little late, but my solution of this problem is:

@Override
public boolean onTouchEvent(MotionEvent event) {
    this.mDetector.onTouchEvent(event);
    return super.onTouchEvent(event);
}

The return of the Detector gone lost. Change to:

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (this.mDetector != null) return this.mDetector.onTouchEvent(event);
   return super.onTouchEvent(event);
}

But is this a good solution? Time for Android Insider. ;)

Tschau Fred

like image 26
Fred Avatar answered Nov 15 '22 17:11

Fred