listView.setSelection(0);



  1. public class MainActivity extends Activity implements OnTouchListener{  

  2.   

  3.     private GestureDetector mGestureDetector;     

  4.       

  5.   

  6.     @Override  

  7.     protected void onCreate(Bundle savedInstanceState) {  

  8.         super.onCreate(savedInstanceState);  

  9.         setContentView(R.layout.activity_main);  

  10.           

  11.   

  12.       mGestureDetector = new GestureDetector(new gestureListener()); //使用派生自OnGestureListener  

  13.       mGestureDetector.setOnDoubleTapListener(new doubleTapListener());  

  14.           

  15.       TextView tv = (TextView)findViewById(R.id.tv);  

  16.       tv.setOnTouchListener(this);  

  17.       tv.setFocusable(true);     

  18.       tv.setClickable(true);     

  19.       tv.setLongClickable(true);   

  20.     }  

  21.       

  22.       

  23.     /*  

  24.      * 在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector  

  25.      * 来分析是否有合适的callback函数来处理用户的手势  

  26.      */    

  27.     public boolean onTouch(View v, MotionEvent event) {  

  28.         return mGestureDetector.onTouchEvent(event);     

  29.     }  

  30.       

  31.     //OnGestureListener监听  

  32.     private class gestureListener implements GestureDetector.OnGestureListener{  

  33.   

  34.         public boolean onDown(MotionEvent e) {  

  35.             Log.i("MyGesture""onDown");     

  36.             Toast.makeText(MainActivity.this"onDown", Toast.LENGTH_SHORT).show();     

  37.             return false;  

  38.         }  

  39.   

  40.         public void onShowPress(MotionEvent e) {  

  41.             Log.i("MyGesture""onShowPress");     

  42.             Toast.makeText(MainActivity.this"onShowPress", Toast.LENGTH_SHORT).show();     

  43.         }  

  44.   

  45.         public boolean onSingleTapUp(MotionEvent e) {  

  46.             Log.i("MyGesture""onSingleTapUp");     

  47.             Toast.makeText(MainActivity.this"onSingleTapUp", Toast.LENGTH_SHORT).show();     

  48.             return true;     

  49.         }  

  50.   

  51.         public boolean onScroll(MotionEvent e1, MotionEvent e2,  

  52.                 float distanceX, float distanceY) {  

  53.             Log.i("MyGesture22""onScroll:"+(e2.getX()-e1.getX()) +"   "+distanceX);     

  54.             Toast.makeText(MainActivity.this"onScroll", Toast.LENGTH_LONG).show();     

  55.               

  56.             return true;     

  57.         }  

  58.   

  59.         public void onLongPress(MotionEvent e) {  

  60.              Log.i("MyGesture""onLongPress");     

  61.              Toast.makeText(MainActivity.this"onLongPress", Toast.LENGTH_LONG).show();     

  62.         }  

  63.   

  64.         public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  

  65.                 float velocityY) {  

  66.             Log.i("MyGesture""onFling");     

  67.             Toast.makeText(MainActivity.this"onFling", Toast.LENGTH_LONG).show();     

  68.             return true;  

  69.         }  

  70.     };  

  71.       

  72.     //OnDoubleTapListener监听  

  73.     private class doubleTapListener implements GestureDetector.OnDoubleTapListener{  

  74.   

  75.         public boolean onSingleTapConfirmed(MotionEvent e) {  

  76.             Log.i("MyGesture""onSingleTapConfirmed");     

  77.             Toast.makeText(MainActivity.this"onSingleTapConfirmed", Toast.LENGTH_LONG).show();    

  78.             return true;  

  79.         }  

  80.   

  81.         public boolean onDoubleTap(MotionEvent e) {  

  82.             Log.i("MyGesture""onDoubleTap");     

  83.             Toast.makeText(MainActivity.this"onDoubleTap", Toast.LENGTH_LONG).show();    

  84.             return true;  

  85.         }  

  86.   

  87.         public boolean onDoubleTapEvent(MotionEvent e) {  

  88.             Log.i("MyGesture""onDoubleTapEvent");     

  89.             Toast.makeText(MainActivity.this"onDoubleTapEvent", Toast.LENGTH_LONG).show();    

  90.             return true;  

  91.         }  

  92.     };  

  93. }  



参考: 

用户手势检测-GestureDetector使用详解