Reordering A RecyclerView Using Drag & Drop

Nathan Sokalski 4,116 Reputation points
2021-04-12T02:28:04.933+00:00

I have a Xamarin.Android app containing a RecyclerView in which I want to allow the user to reorder the items using Drag & Drop. I know the basics of Drag & Drop using the StartDragAndDrop method & Drag event. I call StartDragAndDrop from the LongClick event of the item I want to move. However, here are some of the things I am having trouble figuring out how to do:

  1. Determine the Source of the event during the Entered & Exited DragAction. Because ClipData is only available during the Drop DragAction, I was unable to use that. I need this information during Entered & Exited because I want to visually shift the other items so the user can see where this item will be placed.
  2. Customizing the DragShadowBuilder. I need to do this because I want the Drag Shadow to be opaque, so that the user appears to be dragging the actual item.
  3. Restricting the drag motion to vertical. Because the items are being reordered (as opposed to other scenarios where items may get dragged to different boxes or areas), I do not what the items being moved horizontally. I am not sure what the best way to do this is, although the two ideas crossing my mind were dynamically changing the layout margins of the item or dynamically modifying the Shadow Metrics of the Drag Shadow.

Are there any good examples of ways to reorder a RecyclerView? What is the best way to access drag source data in the Entered & Exited DragAction(s)? Are there any good examples of a custom DragShadowBuilder (the documentation did not give any good examples)? Are there any easy ways to restrict dragging to vertical? Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,294 questions
0 comments No comments
{count} votes

Accepted answer
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 68,651 Reputation points Microsoft Vendor
    2021-04-12T07:48:28.03+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    Are there any easy ways to restrict dragging to vertical?

    If you use RecyclerView in Xamarin.Android, you can achieve an interface ItemTouchHelper.Callback , it comes from AndroidX.RecyclerView.Widget. If we want to restrict dragging to vertical. Override the GetMovementFlags method, add move restriction by const int dragFlags = ItemTouchHelper.Up | ItemTouchHelper.Down; and set ItemTouchHelper.ActionStateIdle, then return it by MakeMovementFlags method.

       public class SimpleItemTouchHelperCallback : ItemTouchHelper.Callback  
           {  
               private readonly ITemTouchHelperAdapter _mAdapter;  
         
               public SimpleItemTouchHelperCallback(ITemTouchHelperAdapter adapter)  
               {  
                   _mAdapter = adapter;  
               }  
         
               public override int GetMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder)  
               {  
                   const int dragFlags = ItemTouchHelper.Up | ItemTouchHelper.Down;  
                   const int swipeFlags = ItemTouchHelper.ActionStateIdle;  
                   return MakeMovementFlags(dragFlags, swipeFlags);  
               }  
         
               public override bool OnMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)  
               {  
                   if (viewHolder.ItemViewType != target.ItemViewType)  
                   {  
                       return false;  
                   }  
         
                   // Notify the adapter of the move  
                   _mAdapter.OnItemMove(viewHolder.AdapterPosition, target.AdapterPosition);  
                   return true;  
               }  
         
               public override void OnSwiped(RecyclerView.ViewHolder viewHolder, int direction)  
               {  
                   // Notify the adapter of the dismissal  
                   _mAdapter.OnItemDismiss(viewHolder.AdapterPosition);  
               }  
           }  
    

    Are there any good examples of ways to reorder a RecyclerView?

    If you want to this, please Google: ReOrder the list items by drag and drop in xamarin android using RecyclerView

    Please note: If you want to make above simple work in androidX, please create a new project by VS, then copy the code to this new project.

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful