实现监听左右滑动的事件

package com.cnmobile.beibei.utils;

import android.content.Context;  
import android.view.GestureDetector.SimpleOnGestureListener;  
import android.view.GestureDetector;  
import android.view.View;  
import android.view.View.OnTouchListener;  
import android.view.MotionEvent;  
  
/** 
 * 实现监听左右滑动的事件,哪个view需要的时候直接setOnTouchListener就可以用了 
 * @author LinZhiquan 
 * 
 */  
public class GestureListener extends SimpleOnGestureListener implements OnTouchListener {  
    /** 左右滑动的最短距离 */  
    private int distance = 100;  
    /** 左右滑动的最大速度 */  
    private int velocity = 200;  
      
    private GestureDetector gestureDetector;  
      
    public GestureListener(Context context) {  
        super();  
        gestureDetector = new GestureDetector(context, this);  
    }  
  
    /** 
     * 向左滑的时候调用的方法,子类应该重写 
     * @return 
     */  
    public boolean left() {  
        return false;  
    }  
      
    /** 
     * 向右滑的时候调用的方法,子类应该重写 
     * @return 
     */  
    public boolean right() {  
        return false;  
    }  
      
    @Override  
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
            float velocityY) {  
        // TODO Auto-generated method stub  
        // e1:第1个ACTION_DOWN MotionEvent  
        // e2:最后一个ACTION_MOVE MotionEvent  
        // velocityX:X轴上的移动速度(像素/秒)  
        // velocityY:Y轴上的移动速度(像素/秒)  
  
        // 向左滑  
        if (e1.getX() - e2.getX() > distance  
                && Math.abs(velocityX) > velocity) {  
            left();  
        }  
        // 向右滑  
        if (e2.getX() - e1.getX() > distance  
                && Math.abs(velocityX) > velocity) {  
            right();  
        }  
        return false;  
    }  
  
    @Override  
    public boolean onTouch(View v, MotionEvent event) {  
        // TODO Auto-generated method stub  
        gestureDetector.onTouchEvent(event);  
        return false;  
    }  
  
    public int getDistance() {  
        return distance;  
    }  
  
    public void setDistance(int distance) {  
        this.distance = distance;  
    }  
  
    public int getVelocity() {  
        return velocity;  
    }  
  
    public void setVelocity(int velocity) {  
        this.velocity = velocity;  
    }  
  
    public GestureDetector getGestureDetector() {  
        return gestureDetector;  
    }  
  
    public void setGestureDetector(GestureDetector gestureDetector) {  
        this.gestureDetector = gestureDetector;  
    }  
}  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值