我有一个 ImageView 需要在投掷/幻灯片上更改图像并在 onclicks 时执行其他操作但我无法管理获取 onclick 以触发,投掷工作很好
这是 Listener 的课程
public abstract class OnFlingGestureListener implements OnTouchListener {
private Context context;
private final GestureDetector gdt = new GestureDetector(this.context, new GestureListener());
@Override
public boolean onTouch(final View v, final MotionEvent event) {
gdt.onTouchEvent(event);
return true;
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
onRightToLeft();
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
onLeftToRight();
return true;
}
if(e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
onBottomToTop();
return true;
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY) {
onTopToBottom();
return true;
}
return false;
}
}
public boolean onDown(MotionEvent e){
return false;
}
public boolean onSingleTapConfirmed(MotionEvent e) {
// ---Call it directly---
click();
return true;
}
public abstract void onRightToLeft();
public abstract void onLeftToRight();
public abstract void onBottomToTop();
public abstract void onTopToBottom();
public abstract void click();
}
然后在我的活动 onCreate 我这样做
ImageView btn = (ImageView) findViewById(R.id.imageButton1);
btn.setOnTouchListener(new OnFlingGestureListener() {
@Override
public void onTopToBottom() {
//Your code here
}
@Override
public void onRightToLeft() {
NextCall();
}
@Override
public void onLeftToRight() {
PrevCall();
}
@Override
public void onBottomToTop() {
//Your code here
}
@Override
public void click() {
ClickCode();
}
});
任何想法我做错了什么?