[英]android detect continue up-down scroll
I am developing application with custom list. 我正在开发自定义列表的应用程序。 I am overriding touch event of the list. 我压倒了列表中的触摸事件。 list scrolls nicely if I scroll once ( touch and move finger up or down once). 如果我滚动一次(触摸并向上或向下移动手指一次),列表滚动得很好。 When I scroll list up - down without leaving the list (ie touch move up then down and then leave), in this case list is not getting scrolled properly. 当我向上滚动列表而不离开列表(即触摸向上移动然后向下然后离开)时,在这种情况下列表没有正确滚动。 As action_move called for first scroll only, ie for up when I move list down action_move is not getting detected. 由于action_move仅调用了第一个滚动,即当我向下移动列表时向上动作,因此没有检测到action_move。 Please let me know if anybody tried to do this. 如果有人试图这样做,请告诉我。 Any code snippet will be of great help. 任何代码段都会有很大的帮助。
Thanks 谢谢
i did something like this. 我做了这样的事。 You can also try. 你也可以试试。 Its working nice 它的工作很好
public boolean onTouchEvent(MotionEvent ev) {
final int action = ev.getAction();
if ((action == MotionEvent.ACTION_MOVE) && (mTouchState != TOUCH_STATE_REST)) {
Log.d("DB", " scrolling twice ");
scrolltwice = true;
return true;
}
final float x = ev.getX();
final float y = ev.getY();
switch(action & MotionEvent.ACTION_MASK){
case MotionEvent.ACTION_MOVE:
final int xDiff = (int) Math.abs(x - mLastMotionX);
yDiff = (int) (y - mLastMotionY);
final int ydif = (int) Math.abs(y - mLastMotionY);
final int touchSlop = mTouchSlop;
boolean xMoved = xDiff > touchSlop;
boolean yMoved = ydif > touchSlop;
if (xMoved || yMoved) {
if (yMoved) {
mTouchState = TOUCH_STATE_SCROLLING;
}
}
break;
case MotionEvent.ACTION_DOWN:
if (!mScroller.isFinished()) {
mScroller.abortAnimation();
}
// Remember location of down touch
mLastMotionX = x;
mLastMotionY = y;
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
final int myDiff = (int) (y - mLastMotionY);
if(mTouchState == TOUCH_STATE_SCROLLING){
scrolltwice = false;
if(!scrolltwice){
Log.d("DB", " ACTION_UP fetch new records ");
FetchRecords rec = new FetchRecords();
rec.execute();
if(yDiff < 0){ // fetching next slot of records
nextRecordId = nextRecordId + previousTotal;
if(nextRecordId > totalRowCount){
nextRecordId = nextRecordId - previousTotal;
}
}else if(yDiff > 0){ // fetching previous slot of records
nextRecordId = nextRecordId - previousTotal;
if(nextRecordId < 1){
nextRecordId = 0;
}
}
}
}
scrolltwice = false;
mTouchState = TOUCH_STATE_REST;
break;
}
return false;
}
//implement ontouch listener if the view is list pass it onTouchEvent
public boolean onTouch(View v, MotionEvent event) {
if(v.equals(objListView))
onTouchEvent(event);
return false;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.