读书人

listview onClick longClick onTouch三

发布时间: 2012-09-17 12:06:51 作者: rapoo

listview onClick longClick onTouch三个事件的处理

int lastX, curX; private int totalMove = 0; private boolean firstDown = true;//开关 int duration = 150; OnTouchListener listViewOnTouchListener = new OnTouchListener() {    @Override  public boolean onTouch(View v, MotionEvent event) {   switch (event.getAction()) {   case MotionEvent.ACTION_DOWN: {    lastX = (int)event.getX();    totalMove = 0;    firstDown = false;        return false;   }   case MotionEvent.ACTION_MOVE:{    if (firstDown) {     curX = (int) event.getX();     totalMove = 0;     firstDown = false;    }    curX = (int) event.getX();    int delatX = curX - lastX;    //if (delatX > 0) {     totalMove += delatX;     lastX = curX;    //}    return false;   }   case MotionEvent.ACTION_UP:{    boolean result = false;    if(totalMove > 20 ){     //     Log.e("right","right");     totalMove = 0;//things you shouold do here     result = true;    }    if(totalMove < 0 && Math.abs(totalMove) > 20){     Log.e("left","left");     totalMove = 0;//things you shouold do here     result= true;    }    return result;   }   }   return false;     } };listView.setOnTouchListener(listViewOnTouchListener ); 其他的onclickListener onLongClickListener 好普通 无需介绍关键在于onTouchListener中 onDown的时候 返回false onCLICK 与longclick也会感应到这个动作,true就相反。。。然后在onMove的时候去统计移动的距离,设定一个滑动的敏感度,达到这个值就返回false 处理相应的操作,例如划屏更新UI。。。不过这个值就返回true让onclick的事件响应,同时设定一个duration时延给longclick事件 达到某个值的话 也直接返回true。。 没有直接的api方法让他们共存,在于我们如何使用而已。

?

1 楼 大柳树 2011-06-23 帅!!正好用到这!! 2 楼 bawanglb 2012-02-29 支持楼主,挺好

读书人网 >移动开发

热点推荐