【问题标题】:how to slide view on finger position如何在手指位置滑动视图
【发布时间】:2014-03-06 21:32:32
【问题描述】:

我有一个自定义列表视图,当用户的手指仍在屏幕上时,我需要能够上下滑动。如何才能做到这一点?我在自定义列表视图类中提供了 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);
}

【问题讨论】:

    标签: android android-listview touch-event ontouchlistener


    【解决方案1】:

    我认为您需要列表视图的setScrollY() 以匹配您的onTouch 侦听器捕获的当前y

     @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);
    }
    

    【讨论】:

    • 不,它不起作用。当我向下拖动手指时,我需要能够向下拖动整个列表视图,当我向上移动手指时,我需要能够向后拖动
    • 做了一些改变。现在引用列表视图,不确定它的变量名来自您的代码,并根据 ACTION_MOVE 更改为增量滚动。
    猜你喜欢
    • 2019-04-28
    • 1970-01-01
    • 1970-01-01
    • 2014-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多