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;
}


}就可以了

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值