java – How to limit drag and drop only to certain views?

Question:

I would like to know if I can control what goes into my layout (DROP) Because from the image view below I would like only "imgu" to be able to enter my layout, the rest not:

findViewById(R.id.imga).setOnLongClickListener(new MyOnLongClickListener());
    findViewById(R.id.imge).setOnLongClickListener(new MyOnLongClickListener());
    findViewById(R.id.imgi).setOnLongClickListener(new MyOnLongClickListener());
    findViewById(R.id.imgo).setOnLongClickListener(new MyOnLongClickListener());
    findViewById(R.id.imgu).setOnLongClickListener(new MyOnLongClickListener());

    findViewById(R.id.layoutrecebe).setOnDragListener( new MyOnDragListener(1));

Answer:

In the onDrag() method, of your OnDragListener implementation, check if the id of the received view is the one you want, if not, return false :

@Override
public boolean onDrag(View v, DragEvent event) {

    if(v.getId() != R.id.imgu) return false;
    ...
    ...
}
Scroll to Top