Android使用OnLongClickListener ListView实现OnTouch

时间:2014-07-04 23:46:52

标签: android listview onlongclicklistener ontouch

我试图将ListViewRemovalAnimation(http://developer.android.com/shareables/devbytes/ListViewRemovalAnimation.zip)与Cell Dragging&重新排列(http://developer.android.com/shareables/devbytes/ListViewDraggingAnimation.zip

当用户在onTouch中按下视图x秒秒并因此调用LongClick时,我试图返回false。

然而,滑动删除似乎工作得很好,但我似乎找不到在需要时调用onLongClickListener的方法。我尝试在用户第一次按下视图和当前时间之间获得时间差,但这似乎不起作用。我还考虑过使用Handler和一个可运行的Handler,但我想不出一种方法可以达到我想要的效果。

我有一种感觉,我正在从错误的角度接近这个,但任何帮助,通过拖动和重新排列列表视图来滑动删除将不胜感激:)

以下是OnTouch的代码:

public boolean onTouch(final View v, MotionEvent event) {
        time = event.getDownTime();
        if (mSwipeSlop < 0) {
            mSwipeSlop = ViewConfiguration.get(ListViewDraggingAnimation.this).
                    getScaledTouchSlop();
        }
        if(SystemClock.uptimeMillis() - time > 2000) {
            return false;
        }
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                if (mAnimating) {
                    // Multi-item swipes not handled
                    return false;
                }
                mItemPressed = true;
                mDownX = event.getX();
                break;
            case MotionEvent.ACTION_CANCEL:
                setSwipePosition(v, 0);
                mItemPressed = false;
                break;
            case MotionEvent.ACTION_MOVE:
            {
                ifNameTaken.setText("debug69");
                if (mAnimating) {
                    return false;
                }
                float x = event.getX();
                if (isRuntimePostGingerbread()) {
                    x += v.getTranslationX();
                }
                float deltaX = x - mDownX;
                float deltaXAbs = Math.abs(deltaX);
                if (!mSwiping) {
                    if (deltaXAbs > mSwipeSlop) {
                        mSwiping = true;
                        listView.requestDisallowInterceptTouchEvent(true);
                    }
                }
                if (mSwiping) {
                    setSwipePosition(v, deltaX);
                }
            }
            break;
            case MotionEvent.ACTION_UP:
            {
                if (mAnimating) {
                    return false;
                }
                // User let go - figure out whether to animate the view out, or back into place
                if (mSwiping) {
                    float x = event.getX();
                    if (isRuntimePostGingerbread()) {
                        x += v.getTranslationX();
                    }
                    float deltaX = x - mDownX;
                    float deltaXAbs = Math.abs(deltaX);
                    float fractionCovered;
                    float endX;
                    final boolean remove;
                    if (deltaXAbs > v.getWidth() / 4) {
                        // Greater than a quarter of the width - animate it out
                        fractionCovered = deltaXAbs / v.getWidth();
                        endX = deltaX < 0 ? -v.getWidth() : v.getWidth();
                        remove = true;
                    } else {
                        // Not far enough - animate it back
                        fractionCovered = 1 - (deltaXAbs / v.getWidth());
                        endX = 0;
                        remove = false;
                    }
                    // Animate position and alpha
                    long duration = (int) ((1 - fractionCovered) * SWIPE_DURATION);
                    animateSwipe(v, endX, duration, remove);
                } else {
                    mItemPressed = false;
                }
            }
            break;
            default:
                return false;
        }
        return true;
    }

我也没有编辑任何ListViewDraggingAnimation OnLongClick代码。

编辑:我尝试在SystemClock.uptimeMillis() - event.getDownTime()&gt;之间进行比较。 2000在交换机之外,认为Action_Down仅在用户按下时才执行,但仍然没有返回false。我认为问题可能与逻辑比较本身有关

1 个答案:

答案 0 :(得分:0)

使用Log.d()通过logcat检查变量的值,听起来就像测试时一样。

onLongClick将是此onTouch函数之外的函数