I did a drag and drop GridView. Everything works fine except for the first drag and drop. I can not find the reason why this happens only for the first time.
The first time I drag an item over other items in a gridview, image sources of images are lost. When I reset it, the sources will be fixed. After that, everything will work fine. Any idea how I can fix this or the cause of the weird behavior. Every help is appreciated.
here is my touchListener that will handle drag and drop:
mGridView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
GridView parent = (GridView) v;
int x = (int) event.getX();
int y = (int) event.getY();
int position = parent.pointToPosition(x, y);
if (position > AdapterView.INVALID_POSITION) {
int count = parent.getChildCount();
for (int i = 0; i < count; i++) {
View curr = parent.getChildAt(i);
curr.setOnDragListener(new View.OnDragListener() {
@Override
public boolean onDrag(View v, DragEvent event) {
boolean result = true;
int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
break;
case DragEvent.ACTION_DRAG_LOCATION:
break;
case DragEvent.ACTION_DRAG_ENTERED:
setBlueFilterOnImageView((ImageView) v, getActivity());
break;
case DragEvent.ACTION_DRAG_EXITED:
clearColorFilterOnImageView((ImageView) v);
break;
case DragEvent.ACTION_DROP:
if (event.getLocalState() == v) {
result = false;
} else {
View droped = (View) event.getLocalState();
View target = v;
app.myProfile().removeProfilePhotoAtIndex(((DragGridItemHolder) droped.getTag()).position);
app.myProfile().insertProfilePhoto(((DragGridItemHolder) droped.getTag()).id, ((DragGridItemHolder) target.getTag()).position);
mAdapter.notifyDataSetChanged();
}
break;
case DragEvent.ACTION_DRAG_ENDED:
clearColorFilterOnImageView((ImageView) v);
break;
default:
result = false;
break;
}
return result;
}
});
}
int relativePosition = position - parent.getFirstVisiblePosition();
View target = (View) parent.getChildAt(relativePosition);
DragGridItemHolder holder = (DragGridItemHolder) target.getTag();
String text = holder.id;
ClipData data = ClipData.newPlainText("DragData", text);
target.startDrag(data, new View.DragShadowBuilder(target), target, 0);
}
}
return false;
}
});
EDIT
public static void setBlueFilterOnImageView(ImageView imageView, Context context){
PorterDuffColorFilter blueFilter = new PorterDuffColorFilter(
context.getResources().getColor(R.color.grid_state_focused),
PorterDuff.Mode.SRC_ATOP);
imageView.setColorFilter(blueFilter);
imageView.setBackgroundColor(context.getResources().getColor(
R.color.grid_state_pressed));
}
source
share