现在的位置: 首页 > 综合 > 正文

onclick longclick onTouch 共存

2014年01月19日 ⁄ 综合 ⁄ 共 1457字 ⁄ 字号 评论关闭
Java代码  收藏代码
  1. int lastX, curX;  
  2.  private int totalMove = 0;  
  3.  private boolean firstDown = true;//开关  
  4.  int duration = 150;  
  5.  OnTouchListener listViewOnTouchListener = new OnTouchListener() {  
  6.     
  7.   @Override  
  8.   public boolean onTouch(View v, MotionEvent event) {  
  9.    switch (event.getAction()) {  
  10.    case MotionEvent.ACTION_DOWN: {  
  11.     lastX = (int)event.getX();  
  12.     totalMove = 0;  
  13.     firstDown = false;  
  14.       
  15.     return false;  
  16.    }  
  17.    case MotionEvent.ACTION_MOVE:{  
  18.     if (firstDown) {  
  19.      curX = (int) event.getX();  
  20.      totalMove = 0;  
  21.      firstDown = false;  
  22.     }  
  23.     curX = (int) event.getX();  
  24.   
  25.     int delatX = curX - lastX;  
  26.     //if (delatX > 0) {  
  27.      totalMove += delatX;  
  28.      lastX = curX;  
  29.     //}  
  30.     return false;  
  31.    }  
  32.    case MotionEvent.ACTION_UP:{  
  33.     boolean result = false;  
  34.     if(totalMove > 20 ){  
  35.      //  
  36.      Log.e("right","right");  
  37.      totalMove = 0;  
  38. //things you shouold do here  
  39.      result = true;  
  40.     }  
  41.     if(totalMove < 0 && Math.abs(totalMove) > 20){  
  42.      Log.e("left","left");  
  43.      totalMove = 0;  
  44. //things you shouold do here  
  45.   
  46.      result= true;  
  47.     }  
  48.     return result;  
  49.    }  
  50.    }  
  51.    return false;  
  52.      
  53.   }  
  54.  };  
  55.   
  56. listView.setOnTouchListener(listViewOnTouchListener );  
  57.   
  58.  
  59. 关键在于onTouchListener中 onDown的时候 返回false onCLICK 与longclick也会感应到这个动作,true就相反。。。然后在onMove的时候去统计移动的距离,设定一个滑动的敏感度,达到这个值就返回false 处理相应的操作,例如划屏更新UI。。。不过这个值就返回true让onclick的事件响应,同时设定一个duration时延给longclick事件 达到某个值的话 也直接返回true。。
     

抱歉!评论已关闭.