0

I am trying to implement drag and drop to create a grammatical exercise in which you have to complete the sentence with the right word, dragging the right answer in the right spot (as shown in figure below). enter image description here

I tried to achieve that by using custom renderer, creating some extended native views that override the methods to implement their dragging and dropping (like OnTouchEvent for Android, and TouchesBegan, TouchesMoved, TouchesEnded for iOS). In both platform I am able to drag the custom native views, but only in iOS I am able to drop them into the specific destination view that I want. In Android it seems to be a conflict with Xamarin Forms view coordinates and the native Android coordinates used in the android custom renderer. Even if I try to "adjust" them, (by manually inserting the coordinates that I want the view to be dropped in) it seems like that the check if(destinationRect.Contains(myTouchX, myTouchY) doesn't work proper.

Git repository example here: Git example

Does anyone know how to solve that? Or does anyone know how to implement drag and drop (with a proper dropzone to check) in any other easiest way?

Poli97
  • 305
  • 4
  • 18

1 Answers1

2

In Android, it provide the View.StartDrag(ClipData, View+DragShadowBuilder, Object, Int32) Method method to start a drag and drop operation.

You could check the MS document.

View.StartDrag(ClipData, View+DragShadowBuilder, Object, Int32) Method: https://learn.microsoft.com/en-us/dotnet/api/android.views.view.startdrag?view=xamarin-android-sdk-9

View.StartDragAndDrop(ClipData, View+DragShadowBuilder, Object, Int32) Method: https://learn.microsoft.com/en-us/dotnet/api/android.views.view.startdraganddrop?view=xamarin-android-sdk-9

Based on my search, this blog would be helpful for this method. https://pumpingco.de/blog/adding-drag-and-drop-to-your-android-application-with-xamarin/

You could download the source file from the GitHub. https://github.com/Pumpingcode/Xamarin-DragAndDropDemo

enter image description here

Wendy Zang - MSFT
  • 10,509
  • 1
  • 7
  • 17