android 在拖放时滚动

标签 android listview drag-and-drop

您好,我正在尝试在 ListView 上进行拖放操作。它很好用。但是我失去了点击和滚动的能力。我如何保留这两个?我在想拖放应该发生在长按项目而不是正常按下时。这是我的 onTouch,它位于扩展 ListView 的 MyListView 中

@Override
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = ev.getAction();
        final int x = (int) ev.getX();
        final int y = (int) ev.getY();  

        if (action == MotionEvent.ACTION_DOWN && x < this.getWidth()/4) {
            this._dragMode = true;
        }

        if (!this._isDragNDrop) 
            return super.onTouchEvent(ev);

        switch (action) {
            case MotionEvent.ACTION_DOWN:
                this._startPosition = pointToPosition(x,y);
                if (this._startPosition != INVALID_POSITION) {
                    int mItemPosition = this._startPosition - getFirstVisiblePosition();
                    this._dragPointOffset = y - getChildAt(mItemPosition).getTop();
                    this._dragPointOffset -= ((int)ev.getRawY()) - y;
                    startDrag(mItemPosition,y);
                    drag(x,y);
                }   
                break;
            case MotionEvent.ACTION_MOVE:
                drag(x,y);
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
            default:
                this._dragMode = false;
                //_isDragNDrop = false;
                this._endPosition = pointToPosition(x,y);
                stopDrag(this._startPosition - getFirstVisiblePosition());
                if (this._dropListener != null && this._startPosition != INVALID_POSITION && this._endPosition != INVALID_POSITION) 
                    this._dropListener.onDrop(this._startPosition, this._endPosition);
                break;
        }
        return true;
    }

最佳答案

我刚刚在我的应用程序中实现了拖放 ListView ,拖动是通过长按一个项目开始的。

源代码在 GitHub 上:DraggableListView , 我有一个 blog post来描述它。希望对你有帮助。

关于android 在拖放时滚动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5427709/

相关文章:

android - 是否可以将 Google Prompt 用于我自己的应用程序?

Android:记住我不工作

Java 拖放至 native 操作系统

android - 使用 CollapsingToolBarLayout 时工具栏标题不可见

Android - 在 ListView 中滚动时滚出/折叠元素

c# - 如何返回 ListView 项目的集合?

Android - ListView 中的单选按钮?

wpf - 在两个相同的应用程序之间进行锣拖放

javascript - 在 javascript 或 jquery 中检测鼠标的当前状态,即向下或向上

android - 排行榜中的分数未更新