Android手势无法使用Image

时间:2017-02-28 23:01:44

标签: android gestures

我试图让滑动手势仅适用于简单的图像。有没有人不为什么它不工作。我得到“ontouchnow”但文本视图中没有显示左侧或右侧。

我列出了以下代码:

public class MainActivity extends AppCompatActivity {

    private GestureDetectorCompat gesyObject;
    private TextView textView2;
    private ImageView imageView;

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

        gesyObject = new GestureDetectorCompat(this, new GesyOne());

        textView2 = (TextView) findViewById(R.id.textView2);
        imageView = (ImageView) findViewById(R.id.imageView);

        imageView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                textView2.setText("ontouchnow");
                return gesyObject.onTouchEvent(event);
            }
        });


    }

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

    class GesyOne extends GestureDetector.SimpleOnGestureListener{

        @Override
        public boolean onFling(MotionEvent event1, MotionEvent event2, float velocityX, float velocityY) {
            if (event2.getX() > event1.getX()) {
                textView2.setText("LEFT");

            } else if (event2.getX() < event1.getX()) {
                textView2.setText("RIGHT");
            }
            return true;
        }

    }

}

2 个答案:

答案 0 :(得分:0)

答案 1 :(得分:0)

我现在就开始工作了。需要添加onDown运动事件返回true。排序