Android笔记之监听左右滑动事件

1.activity监听滑动(只对空白部分监听左右滑动,对组件例如ListView无效)

package com.example.movetest;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;

public class MainActivity extends Activity {

    private GestureDetector mGestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGestureDetector = new GestureDetector(this, new MyGestureListener());
    }

    
    public boolean onTouchEvent(MotionEvent event) {
         return mGestureDetector.onTouchEvent(event);  
    }

    class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
                float velocityY) {
            if (e1.getX() - e2.getX() > 20) {
                Log.i("MainActivity", "To Left");
            }
            if (e2.getX() - e1.getX() > 20) {
                Log.i("MainActivity", "To Right");
            }
            return true;
        }
    }
}

Done!

posted @ 2013-11-16 12:22  行云有影  阅读(1626)  评论(0编辑  收藏  举报