左右滑动监听

今天在做监听左右滑动的时候,在网上找了几篇帖子,然后按照拿上面的操作,但是总是莫名其妙的监听不了,真的是把人都气疯,但是最后凭着我坚强的毅力最后把这个问题解决了,所以特此在这个地方写一篇博客记录一下这个事件。参考博客
首先,你得创建一个android.view.GestureDetector(Gesture:手势Detector:识别)类的对象用来监听手示动作。代码如下

mGestureDetector = new GestureDetector(this, myGestureListener);
  • 1

这里的myGestureListener是GestureDetector.SimpleOnGestureListener的一个对象,用来识别各种手势动作,源码中SimpleOnGestureListener实现的是OnGestureListener, OnDoubleTapListener这两个接口,如果你只是做检测左右滑动可以去只实现OnGestureListener,然后覆盖public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)方法
代码如下:

GestureDetector.SimpleOnGestureListener myGestureListener = new GestureDetector.SimpleOnGestureListener(){
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            Log.e("<--滑动测试-->", "开始滑动");
            float x = e1.getX()-e2.getX();
            float x2 = e2.getX()-e1.getX();
            if(x>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY){
                  Toast.makeText(HuadongTestActivity.this, "向左手势", Toast.LENGTH_SHORT).show();  

                  startActivity(new Intent(HuadongTestActivity.this,HuadongOtherActivity.class));

            }else if(x2>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY){
                 Toast.makeText(HuadongTestActivity.this, "向右手势", Toast.LENGTH_SHORT).show(); 
            }

            return false;
        };
    };

解释一下onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)的四个参数
原来的英文解释

@param e1 The first down motion event that started the fling.
@param e2 The move motion event that triggered the current onFling.
@param velocityX The velocity of this fling measured in pixels per second
along the x axis.
@param velocityY The velocity of this fling measured in pixels per second
along the y axis.

@param e1 表示手势起点的移动事件 可以得到移动的起始位置的坐标
@param e1 当前手势点的移动事件 可以得到移动结束时的位置坐标
@param velocityX 每秒x轴方向移动的像素
@param velocityY 每秒y轴方向移动的像素

float x = e1.getX()-e2.getX();
float x2 = e2.getX()-e1.getX();     if(x>FLING_MIN_DISTANCE&&Math.abs(velocityX)>FLING_MIN_VELOCITY)
  • 1
  • 2

这段代码表示移动的距离大于自己规定的最小距离,并且移动的速度一定的大于最小速度,这样才做处理,向右的手势处理都差不多,看代码就明白。
关于最小距离,和最小速度,那个东西看自己的需求,这里给一个参考的代码

 private static final int FLING_MIN_DISTANCE = 50;   //最小距离 
  private static final int FLING_MIN_VELOCITY = 0;  .//最小速度
  • 1
  • 2

接下来应该将本activity的onTouch事件交给mGestureDetector来处理,
首先让自己的activity实现OnTouchListener接口,代码如下

public class HuadongTestActivity extends Activity implements OnTouchListener
//下面是实现OnTouch方法 并将处理touch时间交给mGestureDetector
@Override
    public boolean onTouch(View v, MotionEvent event) {
        // TODO Auto-generated method stub
        return mGestureDetector.onTouchEvent(event);
    }

如果就这样的话始终完不成既定目标,还做在onCreate(Bundle savedInstanceState) 方法中做下面的操作

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_huadong_test);
        mGestureDetector = new GestureDetector(this, myGestureListener);
        RelativeLayout mRelativeLayout = (RelativeLayout)findViewById(R.id.id_testRelative);//布局的主容器
        mRelativeLayout.setOnTouchListener(this);//将主容器的监听交给本activity,本activity再交给mGestureDetector
         mRelativeLayout.setLongClickable(true);   //必需设置这为true 否则也监听不到手势 
    }

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值