【问题标题】:How to implement Drag and drop in ListView to another Listview?如何在ListView中实现拖放到另一个Listview?
【发布时间】:2011-10-27 11:04:47
【问题描述】:

我正在实现从 ListView 拖放到另一个 ListView,我在这里有示例应用程序,

How to Drag drop Listview item to another Listview

使用上面的应用程序创建了一个应用程序,它可以正常工作到 ListView 的第 4 个图像。在我看来默认情况下 ListView 显示到第 3 个位置(意味着 0、1、2、3),如果我从第一个位置拖动第 2 个位置ListView 并放入第二个 ListView,它显示相同的图像。当我向下滚动时,位置是 4、5、6、7。如果我拖动第 6 个位置,它会占据第 2 个位置。请帮帮我

【问题讨论】:

    标签: android


    【解决方案1】:

    您应该使用 for 循环。然后将数字计数提供给列表并检查每个计数。检查循环的 id 是否相同,如果它们不同,您将直接在存储的位置获得不同的图像。

    public boolean onTouchEvent(MotionEvent ev) {
            // TODO Auto-generated method stub
            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) {
                mDragMode = true;
            }
    
            if (!mDragMode) 
                return super.onTouchEvent(ev);
    
            switch (action) {
                case MotionEvent.ACTION_DOWN:
                    mStartPosition = pointToPosition(x,y);
                    if (mStartPosition != INVALID_POSITION) {
                        int mItemPosition = mStartPosition - getFirstVisiblePosition();
                        mDragPointOffset = y - getChildAt(mItemPosition).getTop();
                        mDragPointOffset -= ((int)ev.getRawY()) - y;
                        startDrag(mItemPosition,y);
                        drag(0,y);// replace 0 with x if desired
                    }   
                    break;
                case MotionEvent.ACTION_MOVE:
                    drag(0,y);// replace 0 with x if desired
                    break;
                case MotionEvent.ACTION_CANCEL:
                case MotionEvent.ACTION_UP:
                default:
                    mDragMode = false;
                    mEndPosition = pointToPosition(x,y);
                    stopDrag(mStartPosition - getFirstVisiblePosition());
                    if (mDropListener != null && mStartPosition != INVALID_POSITION && mEndPosition != INVALID_POSITION) 
                         mDropListener.onDrop(mStartPosition, mEndPosition);
                    break;
            }
            return true;
        }
    

    这将帮助您将代码从一个位置推送到另一个位置。并在 Drag 方法上指定您要推送它的 x&y 位置。

    【讨论】:

      猜你喜欢
      • 2016-11-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-26
      • 2011-09-26
      • 1970-01-01
      • 1970-01-01
      • 2012-11-24
      相关资源
      最近更新 更多