0

我正在使用自定义列表开发应用程序。我正在覆盖列表的触摸事件。如果我滚动一次(触摸并向上或向下移动手指一次),列表滚动得很好。当我向上滚动列表 - 向下滚动而不离开列表(即触摸向上然后向下然后离开),在这种情况下,列表没有正确滚动。由于 action_move 仅在第一次滚动时调用,即当我将列表向下移动时向上,action_move 未被检测到。如果有人试图这样做,请告诉我。任何代码片段都会有很大帮助。

谢谢

4

1 回答 1

2

我做了这样的事情。你也可以试试。它工作得很好

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;
}
于 2012-10-10T09:28:41.640 回答