标签:
1 private GestureDetector gd; 2 3 4 @Override 5 public boolean onTouchEvent(MotionEvent event) { 6 // TODO Auto-generated method stub 7 return gd.onTouchEvent(event); 8 } 9 10 private void initGD() { 11 gd = new GestureDetector(this, new GestureDetector.OnGestureListener() { 12 @Override 13 public boolean onSingleTapUp(MotionEvent e) { 14 // TODO Auto-generated method stub 15 return false; 16 } 17 18 // 这个方法主要是为了告诉用户手指已经按下,可以高亮被按下的区域 19 @Override 20 public void onShowPress(MotionEvent e) { 21 // TODO Auto-generated method stub 22 } 23 24 @Override 25 public boolean onScroll(MotionEvent e1, MotionEvent e2, 26 float distanceX, float distanceY) { 27 // TODO Auto-generated method stub 28 return false; 29 } 30 31 @Override 32 public void onLongPress(MotionEvent e) { 33 // TODO Auto-generated method stub 34 } 35 36 // 当手指滑动时调用的方法 37 @Override 38 public boolean onFling(MotionEvent e1, MotionEvent e2, 39 float velocityX, float velocityY) { 40 // TODO Auto-generated method stub 41 42 if (velocityX > 0) { 43 finish(); 44 return true; 45 } 46 47 return false; 48 } 49 50 // 每一个事件都会先触发这个方法 51 @Override 52 public boolean onDown(MotionEvent e) { 53 // TODO Auto-generated method stub 54 return false; 55 } 56 }); 57 }
当有一些View会拦截并消费掉触摸事件时,需要为这些View注册setOnTouchListener,
交由GestureDetector.onTouchEvent(event); 统一处理
1 ListView listView=(ListView)findViewById(R.id.listViewBeside); 2 3 listView.setOnTouchListener(new View.OnTouchListener(){ 4 5 @Override 6 public boolean onTouch(View v, MotionEvent event) { 7 // TODO Auto-generated method stub 8 leftPanel.getGestureDetector().onTouchEvent(event);//需要这样写! 9 return false; 10 } 11 12 }); 13 14 listView.setOnItemClickListener(new ListView.OnItemClickListener() { 15 16 @Override 17 public void onItemClick(AdapterView<?> parent, View view, 18 int position, long id) { 19 // TODO Auto-generated method stub 20 Log.i("tag", "position=="+position); 21 } 22 });
因为我的手势是定义在leftPanel这个View中的,所以是leftPanel.getGestureDetector().onTouchEvent(event);。
如果你的手势是写在Activity中的,那么就是mGestureDetector.onTouchEvent(event);
这样既能保证在ListView上实现拖动,又能保证ListView上的Item Click.
标签:
原文地址:http://www.cnblogs.com/jinglecode/p/4521529.html