我试图让滑动手势仅适用于简单的图像。有没有人不为什么它不工作。我得到“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;
}
}
}