ImageView sources are missing for the first time when I drag and drop into my GridView

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));
}
+5
source share
3 answers

. , DragEvent.ACTION_DRAG_ENTERED, ImageView . , reset , . , .. Thnx

0

this, , , . , .

, mGridView.setOnTouchListener(new View.OnTouchListener() ... mGridView.setOnTouchListener(new OnTouchListener() ...

+1

, :

. onTouch false, , . , true

target.startDrag(data, new View.DragShadowBuilder(target), target, 0);

b. You put the onDragListener registration in the onTouch event handling method, which I think may be the main cause of this problem. When you delete a dragged item, it may not be registered. Since registering your listener is independent of the source view, you can directly register onDragListener. And make the code like this:

mGridView.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
            ClipData data = ClipData.newPlainText("", "");
            DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
            view.startDrag(data, shadowBuilder, view, 0);
            view.setVisibility(View.INVISIBLE);
            return true;
        } else {
            return false;
        }
    }

};

for(View view : ...){
    view.setOnDragListener(new View.OnDragListener() {
        //put your implementation here
    }
}
0
source

All Articles