我正在尝试识别imageview上的向下滑动手势。我试过GestureDetector
,但现在已经弃用了。我正在使用的示例代码如下。但是,它不会进入onFling()方法。
GestureDetector gestureDetector;
gestureDetector = new GestureDetector(new GestureListener());
view.findViewById(R.id.imageView).setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (gestureDetector.onTouchEvent(event)) {
System.out.println("DONE-------");
updateSwipeAction();
return true;
}
return false;
}
});
class GestureListener extends SimpleOnGestureListener {
private static final int SWIPE_MIN_DISTANCE = 10;
private static final int SWIPE_THRESHOLD_VELOCITY = 10;
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE) {
//From Right to Left
return true;
} else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE) {
//From Left to Right
return true;
}
if (e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE) {
//From Bottom to Top
return true;
} else if (e2.getY() - e1.getY() > SWIPE_MIN_DISTANCE) {
//From Top to Bottom
return true;
}
return false;
}
@Override
public boolean onDown(MotionEvent e) {
//always return true since all gestures always begin with onDown and<br>
//if this returns false, the framework won't try to pick up onFling for example.
return true;
}
}
答案 0 :(得分:1)
试试这个,实现ongestureListener,你可以直接访问onscroll方法
@SuppressLint( “NewApi”)
public class AndroidExplorer extends Activity implements
android.view.GestureDetector.OnGestureListener
{
private Handler mHandler = new Handler();
@Override
protected void onCreate(Bundle mybundle)
{
// TODO Auto-generated method stub
super.onCreate(mybundle);
setContentView(R.layout.main);
}
@Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
// TODO Auto-generated method stub
return false;
}
}