42、通过ontouch检测expandableListview的单击、长按、列表滚动
一、在model定义变量:
public boolean isExpandableListviewScroll = false;//这个是
首先监听expandableListview的滚动:
Expandablelistview_all_waiting_push.setOnScrollListener(new OnScrollListener() { @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // TODO Auto-generated method stub isExpandableListviewScroll = true; } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { // TODO Auto-generated method stub //push_all_waiting_expandable_adapter.isScroll = true; } });
二、在adapter中定义变量:
public boolean flagLongClick = true;
public boolean flagPressed = true;
otl = new View.OnTouchListener() { @Override public synchronized boolean onTouch(View v, MotionEvent ev) { // TODO Auto-generated method stub switch (ev.getAction() & MotionEvent.ACTION_MASK) { case MotionEvent.ACTION_DOWN: mDownX = ev.getX(); mDownY = ev.getY(); lastPressTime = System.currentTimeMillis(); mealDone.isExpandableListviewScroll = false; if(!flagPressed) { flagPressed = true; CommonUtils.LogWuwei(tag,"lastPressTime is "+lastPressTime); new Handler().postDelayed(new Runnable() { @Override public void run() { // TODO Auto-generated method stub CommonUtils.LogWuwei(tag, "flagLongClick is "+flagLongClick); if(flagLongClick && !mealDone.isExpandableListviewScroll) { mealDone.isExpandableListviewScroll = false; btn_whether_packaged.performLongClick(); flagLongClick = false; } } }, 300); } break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: flagPressed = false; if (!isScroll) { if(mealDone.isExpandableListviewScroll) { mealDone.isExpandableListviewScroll = false; return true; } long nowTime = System.currentTimeMillis(); if(nowTime-lastPressTime > 300) { lastPressTime = 0; flagLongClick = true; } else { flagLongClick = false; Log.e(tag, "onClick "); btn_whether_packaged.performClick(); } return false; } else { isScroll = false; return false; } case MotionEvent.ACTION_MOVE: flagPressed = false; flagLongClick = true; if(Math.abs(mDownX-ev.getX()) > 150) { Log.e(tag, "action move is scroll"); isScroll = true; } break; default: return false ; } return false ;
处理流程:
0、如果expandableListview滚动,则isExpandableListviewScroll置为
1、检测到expandableListview的item按下
①记录按下时间 lastPressTime ;
①首先判断上次按下是否弹起,如果弹起,那么启动一个0.3秒后执行的的定时任务,
②定时任务如下:
判断是否长按且expandableListview没有滚动(!isExpandableListviewScroll && flagLongClick)
如果符合条件,将isExpandableListviewScroll置为false,同时响应事先写好的长按事件。
2、那么如何响应单击事件?
单击事件手抬起的时候,也就是在响应MotionEvent.ACTION_UP的时候,手势抬起处理流程:
①flagPressed = true;意思就是本次按下事件已经抬起
②
判断isExpandableListviewScroll是否为真,也就是看是否在滚动,
2、1、如果是滚动,那么就什么不用响应
2、2、获取当前时间,如果当前时间和按下时间差小于0.3秒,那么执行实现写好的单击事件
3、move事件
如果move则认为手抬起、长按事件flag为真(因为手一直未松开)