onFling for a TextView not working

2019-04-08 23:17发布

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.

2条回答
甜甜的少女心
2楼-- · 2019-04-08 23:54

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;
    }
查看更多
Summer. ? 凉城
3楼-- · 2019-04-08 23:58
    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.

查看更多
登录 后发表回答