我正在尝试检测TextView对象的onFling事件。下面的代码非常适合onDown事件,但是无法获取onScroll或onFling事件。有指针吗?

谢谢

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;
            }
        });
    }
}

布局只是LinearLayout中的TextView。

最佳答案

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

我认为您应该在这里返回true。您通过返回false来拒绝指针向下事件,因此不会收到任何后续事件。

10-04 17:11