My application displays pictures. Here I took an example of the implementation of Grid Layouts . I need to implement this functionality so that when dragging an item to another item ( ImageView on ImageView ), the item being dragged is deleted. I found such an implementation here . Connected it with the implementation found and noticed the problem. If I pinch an item and take it to the basket, then everything is fine, it is deleted, but I can’t move this item to another place, I don’t even have a moving animation. I found out that the ClipData class and the onStartDrag() method conflict with each other. The question is: how to implement the functionality so that the elements move among themselves and can be deleted when hovering over another picture, or, say, on the upper part of the screen (on the ActionBar ) to be removed from the list? Here is the main activity code:
public class MainActivity extends Activity implements OnDragListener, OnStartDragListener { private RecyclerListAdapter adapter; private int draggedIndex = -1; private ItemTouchHelper mItemTouchHelper; private RelativeLayout relativeLayout; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); relativeLayout = (RelativeLayout) findViewById(R.id.trashcan_layout); adapter = new RecyclerListAdapter(this, this); RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recycler_view); recyclerView.setHasFixedSize(true); recyclerView.setAdapter(adapter); GridLayoutManager layoutManager = new GridLayoutManager(this, 2); recyclerView.setLayoutManager(layoutManager); recyclerView.addOnItemTouchListener(new RecyclerItemClickListener(this, recyclerView, new RecyclerItemClickListener.OnItemClickListener() { @Override public void onItemClick(View view, int position) { } @Override public void onItemLongClick(View view, int position) { ClipData.Item item = new ClipData.Item((String) relativeLayout.getTag()); ClipData clipData = new ClipData((CharSequence) relativeLayout.getTag(), new String[]{ClipDescription.MIMETYPE_TEXT_PLAIN}, item); relativeLayout.startDrag(clipData, new View.DragShadowBuilder(relativeLayout), null, 0); View trashCan = relativeLayout.findViewById(R.id.trash_can); trashCan.setVisibility(View.VISIBLE); trashCan.setOnDragListener(MainActivity.this); draggedIndex = position; } })); ItemTouchHelper.Callback callback = new SimpleItemTouchHelperCallback(adapter); mItemTouchHelper = new ItemTouchHelper(callback); mItemTouchHelper.attachToRecyclerView(recyclerView); } @Override public void onStartDrag(RecyclerView.ViewHolder viewHolder) { mItemTouchHelper.startDrag(viewHolder); } @Override public boolean onDrag(View view, DragEvent dragEvent) { switch (dragEvent.getAction()) { case DragEvent.ACTION_DRAG_STARTED: // Drag has started // If called for trash resize the view and return true if (view.getId() == R.id.trash_can) { view.animate().scaleX(1.0f); view.animate().scaleY(1.0f); return true; } else // else check the mime type and set the view visibility if (dragEvent.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN)) { view.setVisibility(View.GONE); return true; } else { return false; } case DragEvent.ACTION_DRAG_ENTERED: // Drag has entered view bounds // If called for trash can then scale it. if (view.getId() == R.id.trash_can) { view.animate().scaleX(1.5f); view.animate().scaleY(1.5f); } return true; case DragEvent.ACTION_DRAG_EXITED: // Drag exited view bounds // If called for trash can then reset it. if (view.getId() == R.id.trash_can) { view.animate().scaleX(1.0f); view.animate().scaleY(1.0f); } view.invalidate(); return true; case DragEvent.ACTION_DRAG_LOCATION: // Ignore this event return true; case DragEvent.ACTION_DROP: // Dropped inside view bounds // If called for trash can then delete the item and reload the grid // view if (view.getId() == R.id.trash_can) { RecyclerListAdapter.mItems.remove(draggedIndex); draggedIndex = -1; } adapter.notifyDataSetChanged(); case DragEvent.ACTION_DRAG_ENDED: // Hide the trash can new Handler().postDelayed(new Runnable() { @Override public void run() { findViewById(R.id.trash_can).setVisibility(View.GONE); } }, 100l); if (view.getId() == R.id.trash_can) { view.animate().scaleX(1.0f); view.animate().scaleY(1.0f); } else { view.setVisibility(View.VISIBLE); } // remove drag listeners view.setOnDragListener(null); return true; } return false; } }