Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onFling for a TextView not working

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.

like image 480
Sandy Avatar asked Oct 11 '22 05:10

Sandy


1 Answers

    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.

like image 146
Dmitry Ryadnenko Avatar answered Nov 03 '22 00:11

Dmitry Ryadnenko