Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Drag View and Drop it on RecyclerView item Android

I am developing an android application that has a screen contains the following:

  1. Recycler view that contains categories as the below figure
  2. Separate view at the botton and the user should able to drag it on a RecyclerView item, and after drop the user will show changing at the RecyclerView item data (for example items count at the category)

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

enter image description here

any help is much appreciated

like image 269
Wael Abo-Aishah Avatar asked Feb 06 '17 11:02

Wael Abo-Aishah


People also ask

Is it possible to drag and drop in Recycler view?

Drag and drop in recycler view is quite easy if we don’t have extra content to scroll rather than the list items, but the problems comes to start when we put the recycler view inside the NestedScrollView. So lets figure out what problems comes and how to solve them.

What is the Android drag and drop framework?

The Android drag and drop framework enables you to add interactive drag and drop capabilities to your app. With drag and drop, users can copy or move text, images, objects—any content that can be represented by a URI—from one View to another within an app or, in multi-window mode, between apps. Figure 1. Drag and drop within an app.

What is recyclerview library in Android?

Highly customizable Android library written in Kotlin that uses AndroidX and extends RecyclerView to include extra features, such as support for drag & drop and swipe gestures, among others. It works with vertical, horizontal and grid lists. The creation (and maintenance) of this library requires time and effort.

How to add swipe to recyclerview?

You can use some of the RecyclerView 's "companion" classes: a utility class to add swipe to dismiss and drag & drop support to RecyclerView.


1 Answers

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

like image 54
Drilon Blakqori Avatar answered Sep 19 '22 14:09

Drilon Blakqori