5

I'm trying to detect an onFling event for a TextView object. The code below works perfectly for the onDown event, however it fails to get the onScroll or onFling events. Any pointers?

Thanks

package rob.testapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity
{
    private GestureDetector myGestDetector;
    private TextView mainTextView;

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myGestDetector = new GestureDetector(this, new SimpleOnGestureListener()
        {
            @Override
            public boolean onScroll(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
            {
                mainTextView.setText("just scroll'n");
                return false;
            }

            @Override
            public boolean onDown(MotionEvent e1)
            {
                mainTextView.setText("on down");
                return false;
            }

            @Override
            public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
            {
                mainTextView.setText("on fling");
                return false;
            }
        });

        mainTextView = (TextView)findViewById(R.id.MainText);
        mainTextView.setText("Starting app...");
        mainTextView.setOnTouchListener(new View.OnTouchListener()
        {
            @Override
            public boolean onTouch(View v, MotionEvent event)
            {
                myGestDetector.onTouchEvent(event);
                return false;
            }
        });
    }
}

The layout is just a TextView inside a LinearLayout.

Sandy
  • 2,572
  • 7
  • 40
  • 61

2 Answers2

4
    public boolean onTouch(View v, MotionEvent event)
    {
        myGestDetector.onTouchEvent(event);
        return false;
    }

I think you should return true here. You view rejects pointer down event by returning false and don't receive any subsequent events because of this.

Dmitry Ryadnenko
  • 22,222
  • 4
  • 42
  • 56
  • Actually I just tried the following, which is more or less what you're saying:`public boolean onTouch(View v, MotionEvent event) { return myGestDetector.onTouchEvent(event); }` What is this called? Can you point me to some reference? Thanks alot! – Sandy Apr 22 '11 at 07:14
  • http://developer.android.com/reference/android/view/View.OnTouchListener.html `Returns True if the listener has consumed the event, false otherwise.` – Dr.J Apr 22 '11 at 07:23
  • Now I have the problem that both onDown and onFling are being called for the same "event" (from a human perspective). I want the onDown to set a random text from an array, and the fling to set the next text (this code is not present in the snippet above). How can I get just the onDown or just the onFling??? – Sandy Apr 22 '11 at 08:18
  • Just do some debug, check touch events flow. It shouldn't be much complicated. – Dmitry Ryadnenko Apr 22 '11 at 09:44
0

But in my case it works. I return true in innerclass of textView but i do return false from my TouchEvent.

public class FingureGesture extends Activity implements OnGestureListener,OnDoubleTapListener{
    GestureDetector detector;
    File sdcard = Environment.getExternalStorageDirectory();
    TextView textView;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fingure_gesture);

        detector = new GestureDetector(this,this);
        textView = (TextView) findViewById(R.id.editText1);
        textView.setOnTouchListener(new OnTouchListener() {

            public boolean onTouch(View v, MotionEvent event) {
                Log.i("OnTouch", "Im here ");
                    detector.onTouchEvent(event);
                return false;
            }
        });     
    }//end of onCreate

    public boolean onDown(MotionEvent e) {
        Log.i("onDown ", "x:: "+e.getX()+"\ny:: "+e.getY());
        return false;
    }

    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {  
        return false;
    }


    public boolean onTouchEvent(MotionEvent me){ 

        Log.i(" onTouchEvent", me.toString());
        this.detector.onTouchEvent(me);
//      return super.onTouchEvent(me);
        return true;
    }
    public void onLongPress(MotionEvent e) {
        Log.i(" onLongPress", e.toString());

    }

    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
            float distanceY) {
        Log.i("On Scroll ","\n#e1 :: "+ e1.getX()+"\n#Histo e1.x"+"\n#e2 :: "+e2.getX());
        return false;
    }

    public void onShowPress(MotionEvent e) {
        Log.i(" onShowPress", e.toString());

    }

    public boolean onSingleTapUp(MotionEvent e) {
        Log.i("onSingleTapUp ", e.toString());
        return false;
    }

    public boolean onDoubleTap(MotionEvent e) {
        Log.i(" onDoubleTap", e.toString());
        return false;
    }

    public boolean onDoubleTapEvent(MotionEvent e) {
        Log.i(" onDoubleTapEvent", e.toString());
        return false;
    }

    public boolean onSingleTapConfirmed(MotionEvent e) {
        Log.i(" onSingleTapConfirmed", e.toString());
        return false;
    }
sailor
  • 753
  • 1
  • 6
  • 17