android检测继续上下滚动

时间:2012-10-10 06:46:29

标签: android

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

由于

1 个答案:

答案 0 :(得分: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;
}