我在屏幕上左右滑动有一个事件,但TouchEvent上有一个无效的pointerId = -1,在某些手机上崩溃应用程序这里是我使用的类
public class OnSwipeTouchListener implements OnTouchListener {
private final GestureDetector gestureDetector = new GestureDetector(new GestureListener());
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
// TODO Auto-generated method stub
return gestureDetector.onTouchEvent(arg1);
}
private final class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
@Override
public boolean onDown(MotionEvent e) {
return true;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD && Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
}
public void onSwipeRight() {
}
public void onSwipeLeft() {
}
}
Run Code Online (Sandbox Code Playgroud)
我得到这个错误java.lang.IllegalArgumentException:pointerIndex超出范围
在我的主要活动课中,我使用这个
fullView = (View)findViewById(R.id.fullview);
fullView.setOnTouchListener(imageViewSwiped); -> inside onCreate method
Run Code Online (Sandbox Code Playgroud)
在onCreate之后我使用它
OnTouchListener imageViewSwiped = new OnSwipeTouchListener()
{
public void onSwipeRight() {
minusOne();
}
public void onSwipeLeft() {
// Toast.makeText(MainActivity.this, "left", Toast.LENGTH_SHORT).show();
plusOne();
}
};
Run Code Online (Sandbox Code Playgroud)
我所做的就是评论这个覆盖
/*
@Override
public boolean onDown(MotionEvent e) {
return true;
}
*/
Run Code Online (Sandbox Code Playgroud)
一切都很完美......
| 归档时间: |
|
| 查看次数: |
2065 次 |
| 最近记录: |