I made a drag and drop GridView. Everything works perfectly except for the first drag and drop. I cant find the reason why it only happens the first time.
The first time I drag a item over the other items in the gridview, the imageview sources of the items are lost. When I drop it the sources will be fixed.. After that every thing will work just normal. Any idea how I can fix this or the cause of the strange behavior?
here is my touchListener which will handle the 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));
}
I have found this releated to your query may be it will help you. Its too hard to understand your long code so try to manage from this link.
May be issue is in mGridView.setOnTouchListener(new View.OnTouchListener() ...
once try to do like mGridView.setOnTouchListener(new OnTouchListener() ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With