0

我有一个错误,我不知道如何修复。关键是我应该为我的列表视图实现 DragNDrop 功能,但我的 minSdkVersion="7",所以我使用了Eric Harlow的框架。

当我使用 Android 2.1、2.2、2.3.6 到 4.0 时,一切都以正确的方式运行,但是当我在 Android 4.1.2 上测试我的应用程序时,我遇到了一个错误。问题的核心是:让我假设我的列表视图包含 4 个项目,我将第 4 个项目拖到第 3 个位置。

完成后,我想再次将 4 个项目拖动到 2 个位置。当我第一次从 4 位置拖放到 3 位置时,结果是好的,我的视图无效。但是当我第二次想要拖动 Ndrop 4 项时,我发现在我的手指下我有旧的第 4 元素(现在它是第 3 元素),而不是新元素。

在我的调查中,当我试图让视图对象移动时,我对视图对象的引用有误,View item = getChildAt(itemIndex);但我不知道为什么它在 Android 早期 4.1.2 上可以完美运行,并且在 Android 4.1.2 下可以使用错误。

有人知道如何解决这个错误吗?

下面是对代码的回顾:

 public class DragNDropListView extends ListView {

    private static final int OUR_LOCATION_ELEMENT = 1;
    private static final int MIN_INDEX_ELEM_DONT_TOUCH = 2;
    boolean mDragMode;

    int mStartPosition;
    int mEndPosition;
    int mDragPointOffset;       //Used to adjust drag view location

    ImageView mDragView;

    DropListener mDropListener;
    RemoveListener mRemoveListener;
    DragListener mDragListener;
    UpdateOurLocationListener mOnClickListener;

    Context context;

    public DragNDropListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public void setDropListener(DropListener l) {
        mDropListener = l;
    }

    public void setRemoveListener(RemoveListener l) {
        mRemoveListener = l;
    }

    public void setDragListener(DragListener l) {
        mDragListener = l;
    }

    public void setOnOurLocationListener (UpdateOurLocationListener l) {
        mOnClickListener = l;
    }

    @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()-context.getResources().getDimension(R.dimen.image_move_width))&&
                pointToPosition(x,y) == OUR_LOCATION_ELEMENT) {
                mOnClickListener.onOurLocationClick();
                mDragMode = false;
        }

        if (action == MotionEvent.ACTION_DOWN &&
            x > (this.getWidth()-context.getResources().getDimension(R.dimen.image_move_width))&&
            pointToPosition(x,y)>MIN_INDEX_ELEM_DONT_TOUCH) {
            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 
                        && mEndPosition > MIN_INDEX_ELEM_DONT_TOUCH) {
                    mDropListener.onDrop(mStartPosition, mEndPosition);
                }
                break;
        }
        return true;
    }   

    // move the drag view
    private void drag(int x, int y) {
        if (mDragView != null) {
            WindowManager.LayoutParams layoutParams = (WindowManager.LayoutParams) mDragView.getLayoutParams();
            layoutParams.x = x;
            layoutParams.y = y - mDragPointOffset;
            WindowManager mWindowManager = (WindowManager) getContext()
                    .getSystemService(Context.WINDOW_SERVICE);
            mWindowManager.updateViewLayout(mDragView, layoutParams);

            if (mDragListener != null)
                mDragListener.onDrag(x, y, null);// change null to "this" when ready to use
        }
    }

    // enable the drag view for dragging
    private void startDrag(int itemIndex, int y) {
        stopDrag(itemIndex);
        Log.e ("startDrag " , "itemIndex = " + itemIndex);
        View item = getChildAt(itemIndex);                              //  ???
        Log.e ("startDrag - View item " , "item = " + item.toString());
        if (item == null) return;
        item.setDrawingCacheEnabled(true);
        if (mDragListener != null)
            mDragListener.onStartDrag(item);

        // Create a copy of the drawing cache so that it does not get recycled
        // by the framework when the list tries to clean up memory
        Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());

        WindowManager.LayoutParams mWindowParams = new WindowManager.LayoutParams();
        mWindowParams.gravity = Gravity.TOP;
        mWindowParams.x = 0;
        mWindowParams.y = y - mDragPointOffset;

        mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
        mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        mWindowParams.format = PixelFormat.TRANSLUCENT;
        mWindowParams.windowAnimations = 0;

        Context context = getContext();
        ImageView v = new ImageView(context);
        v.setImageBitmap(bitmap);      

        WindowManager mWindowManager = (WindowManager)context.getSystemService(Context.WINDOW_SERVICE);
        mWindowManager.addView(v, mWindowParams);
        mDragView = v;
    }

    // destroy drag view
    private void stopDrag(int itemIndex) {
        if (mDragView != null) {
            if (mDragListener != null)
                mDragListener.onStopDrag(getChildAt(itemIndex));
            mDragView.setVisibility(GONE);
            WindowManager wm = (WindowManager)getContext().getSystemService(Context.WINDOW_SERVICE);
            wm.removeView(mDragView);
            mDragView.setImageDrawable(null);
            mDragView = null;
        }
    }

}
4

1 回答 1

0

为了解决这个问题,我在调用 getChildAt(position) 之前使用了方法 layoutChildren()

于 2012-11-12T10:08:30.710 回答