Reputation: 942
I am developing an android application that has a screen contains the following:
I need some help about how to implement this process to drag View into Recycler item, the following figure explains exactly what I want to do but have no idea how to do it
any help is much appreciated
Upvotes: 13
Views: 4358
Reputation: 2826
Start by adding a draglistener to your inflated view in onCreateViewHolder
of your recycler adapter.
view.setOnDragListener(new OnDragListener() {
@Override
public boolean onDrag(View view, DragEvent dragEvent) {
switch (dragEvent.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// drag has started, return true to tell that you're listening to the drag
return true;
case DragEvent.ACTION_DROP:
// the dragged item was dropped into this view
Category a = items.get(getAdapterPosition());
a.setText("dropped");
notifyItemChanged(getAdapterPosition());
return true;
case DragEvent.ACTION_DRAG_ENDED:
// the drag has ended
return false;
}
return false;
}
});
In the ACTION_DROP
case you can either change the model and call notifyItemChanged()
, or modify directly the view (that won't handle the rebinding case). Also in onCreateViewHolder
add a longClickListener
to your View
, and in onLongClick
start the drag:
ClipData.Item item = new ClipData.Item((CharSequence) view.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(view.getTag().toString(),
mimeTypes, item);
view.setVisibility(View.GONE);
DragShadowBuilder myShadow = new DragShadowBuilder(view);
if (VERSION.SDK_INT >= VERSION_CODES.N) {
view.startDragAndDrop(dragData, myShadow, null, 0);
} else {
view.startDrag(dragData, myShadow, null, 0);
}
For more information about drag and drop check out the android developers site
Upvotes: 13