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!