1

I'm not sure how to use the parameters of startDragAndDrop(). Here 's what I know:

  • Clipdata data - not important, just create an empty clipdata object and pass it in

  • DragShadowBuilder shadowBuilder - the DragShadow of the view you are about to drag, as the name suggests

  • Object myLocalState - I have no idea what this is and what to pass in?

  • int flags - just set it to 0. Could be useful if you are to drag your view from one application to another for example

So my problem is primarily with Object myLocalState. What could be achieved with the state, could someone give an example?

mbo
  • 4,611
  • 2
  • 34
  • 54
VictorCharlie23
  • 121
  • 1
  • 10

1 Answers1

2

You could use the local state to pass information with your dragged view. This may be useful for the target view behavior. The documentation of startDragAndDrop() says:

myLocalState Object: An Object containing local data about the drag and drop operation. When dispatching drag events to views in the same activity this object will be available through getLocalState(). Views in other activities will not have access to this data (getLocalState() will return null). myLocalState is a lightweight mechanism for the sending information from the dragged View to the target Views. For example, it can contain flags that differentiate between a a copy operation and a move operation.

But you can just pass null to not pass anything.

You find a pretty detailed tutorial on drag & drop here: https://developer.android.com/guide/topics/ui/drag-drop.html

mbo
  • 4,611
  • 2
  • 34
  • 54
  • There's however one tiny complication: I unknowingly upgraded my build to api26, while my target is api 18. You see, for api 18 the function should be startdrag, but it is deprecated in api 26. Can you tell me what would be the best course of action? Can I lower my api build level? – VictorCharlie23 Oct 16 '17 at 08:55
  • Your `targetSdkVersion` should always be the latest one and if your `minSdkVersion` is below 24 you simply use `startDrag()` instead of `startDragAndDrop()` for the lower API levels. How to check the API version is described here: https://stackoverflow.com/a/3940823/3979479 Nevertheless it should work even if it is marked as deprecated. – mbo Oct 16 '17 at 09:05
  • Thanks for the tip, well, the problem is that when I use startDrag, there's a strike line right in the middle. I don't exactly know what it means: it's pretending to be using the old API while actually using the new one? Or it's actually using the old one? Also, I tried using "view" and "null", neither worked. As long as I start dragging, the program enters MotionEvent.ACTION_CANCEL and it stops listening to touch events. – VictorCharlie23 Oct 16 '17 at 09:15
  • Strikethrough means that you're using a method which is marked as deprecated - you could test this by simply adding `@Deprecated` before one of your own methods (so yes, it's using the old method). I cannot tell what the problem is but I'm pretty sure it's not the `localState`. – mbo Oct 16 '17 at 09:19
  • OK that's good enough actually, but how likely do you think it is caused by compatibility issue? Also, do I have to do some coding on the drop target, other than fixing it with a draglistener? – VictorCharlie23 Oct 16 '17 at 09:24
  • Could be anything... I didn't really use the system drag&drop yet... I've always switched to a completely custom drag&drop since the system thing is quite limited... – mbo Oct 16 '17 at 09:39
  • NP bro, you are a real life saver, I gave up on the shadow stuff and I now stick with the ACTION_MOVE and set cords and let the system draw my imageview real time... it works anyway, just a bit weird. Thanks a whole bunch man. – VictorCharlie23 Oct 16 '17 at 09:56