如何在手指位置上滑动视图

时间:2014-03-06 21:32:33

标签: android android-listview ontouchevent ontouchlistener

我有一个自定义列表视图,当用户的手指仍在屏幕上时,我需要能够上下滑动。如何才能做到这一点?我在自定义listview类中提供了ontouchEvent。我试过this.setY(event.getY()),但确实重新开始工作。我怎么能这样做?

    @Override
public boolean onTouchEvent(MotionEvent event) {
    //System.out.println("First ["+this.getFirstVisiblePosition()+"]");

    float y = event.getY();


    switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE: {

            this.setY(event.getY());

            if ( ( y - startY) > THRESHOLD && STATE_REFRESH_ENABLED && !STATE_REFRESHING && Ref_once ) {
                if(mListener!=null) mListener.onRefresh();
                Log.d("CUSTOM LIST"," FOODHOTER LIST LISTENER WORKING");
                this.setClickable(false);
                Ref_once=false;
            }
        }
        break;
        case MotionEvent.ACTION_DOWN: {
            startY = y;
           // STATE_REFRESH_ENABLED = getFirstVisiblePosition() == 0; // We are on the first element so we can enable refresh
                      STATE_REFRESH_ENABLED =listIsAtTop()  ;

            this.setClickable(true);
        }
        case MotionEvent.ACTION_UP: {
            STATE_REFRESHING = false;
            this.setClickable(true);
            Ref_once=true;
        }

    }
    return super.onTouchEvent(event);
}

1 个答案:

答案 0 :(得分:0)

我认为您需要{list}的setScrollY()才能匹配y听众捕获的当前onTouch

 @Override
public boolean onTouchEvent(MotionEvent event) {
    //System.out.println("First ["+this.getFirstVisiblePosition()+"]");

    final float y = event.getY();
    final float scrollY = listview.getScrollY();

    switch (event.getAction()) {
        case MotionEvent.ACTION_MOVE: {

            listview.setScrollY(Y + scrollY);

            if ( ( y - startY) > THRESHOLD && STATE_REFRESH_ENABLED && !STATE_REFRESHING && Ref_once ) {
                if(mListener!=null) mListener.onRefresh();
                Log.d("CUSTOM LIST"," FOODHOTER LIST LISTENER WORKING");
                this.setClickable(false);
                Ref_once=false;
            }
        }
        break;
        case MotionEvent.ACTION_DOWN: {
            startY = y;
           // STATE_REFRESH_ENABLED = getFirstVisiblePosition() == 0; // We are on the first element so we can enable refresh
                      STATE_REFRESH_ENABLED =listIsAtTop()  ;

            this.setClickable(true);
        }
        case MotionEvent.ACTION_UP: {
            STATE_REFRESHING = false;
            this.setClickable(true);
            Ref_once=true;
        }

    }
    return super.onTouchEvent(event);
}