OnGestureListener不执行,监听不到

关于OnGestureListener的详解网上很多不再重复,我再此说一下,如果ViewFlippe控件中的onDown,onFling不执行

就要实现OnTouchListener的onTouch方法

public class HoverPicView extends ViewFlipper implements OnGestureListener,OnTouchListener {


private Context context;
private GestureDetector detector;
private int verticalMinDistance = 50;
private int minVelocity =100;


private static String TAG = "HoverPicView";


public HoverPicView(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}


public void init(Context context) {
this.context = context;
detector = new GestureDetector(this);
System.out.println("pppppp");
}
@Override
public boolean onTouch(View v, MotionEvent event) {
return detector.onTouchEvent(event);
}



@Override
public boolean onDown(MotionEvent e) {
System.out.println("iiiiiii");
return true; // 此处不设置为true onFling()函数可能会不会执行
}


public void startPicAnimation(Integer[] pictures) {
if (pictures != null) {
for (int i = 0; i < pictures.length; i++) {
ImageView imageView = new ImageView(context);
imageView.setImageResource(pictures[i]);
imageView.setOnTouchListener(this);
imageView.setLongClickable(true);
imageView.setScaleType(ScaleType.FIT_XY);
this.addView(imageView, LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
}
}
}


@Override
public void onShowPress(MotionEvent e) {
}


@Override
public boolean onSingleTapUp(MotionEvent e) {
return false;
}


@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
return false;
}


@Override
public void onLongPress(MotionEvent e) {
}


/*
* e1: The first down motion event that started the fling.手势起点的移动事件 e2: The
* move motion event that triggered the current onFling.当前手势点的移动事件
* velocityX: The velocity of this fling measured in pixels per second along
* the x axis.每秒x轴方向移动的像素 velocityY: The velocity of this fling measured in
* pixels per second along the y axis.每秒y轴方向移动的像素
*/
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
Log.i(TAG, "onfling is begin");
// 手势向左滑
if (e1.getX() - e2.getX() > verticalMinDistance
&& Math.abs(velocityX) > minVelocity) {
this.setInAnimation(AnimationUtils.loadAnimation(context,
R.anim.left_in));
this.setOutAnimation(AnimationUtils.loadAnimation(context,
R.anim.left_out));
this.showNext();
return true;
}// 手势向右滑
else if (e2.getX() - e1.getX() > verticalMinDistance
&& Math.abs(velocityX) > minVelocity) { // 向右手势
this.setInAnimation(AnimationUtils.loadAnimation(context,
R.anim.right_in));
this.setOutAnimation(AnimationUtils.loadAnimation(context,
R.anim.right_out));
this.showPrevious();
return true;
}
return false;
}


}就可以了

你可能感兴趣的:(Android)