我有一个我不知道如何解决的错误。关键是我应该为我的listview实现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上完美运行并使用bug在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;
}
}
}
答案 0 :(得分:0)
要解决这个问题,我在调用getChildAt(position)之前使用了layoutChildren()方法