startDrag method Deprecated and unable to compile the program

北战南征 提交于 2019-12-07 05:29:59

问题


startDrag(android.content.ClipData, android.view.View.DragShadowBuilder, java.lang.Object, int) is deprecated. How to solve this without losing compatibility to the older versions? Are there any alternatives? I'm learning android basics and while trying out a simple drag and drop exercise, I encountered this error.


回答1:


According to Androids API reference startDrag() was deprecated in API level 24

Use startDragAndDrop() for newer platform versions.

And since Android API level 24 equals Android N you can use:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
    view.startDragAndDrop(...);
} else {
    view.startDrag(...);
}



回答2:


startDrag was very recently deprecated - in API 24. So you could use startDragAndDrop instead and differentiate between versions.

What you could also say is preserving compatibility to lower versions. The thing is Drag&Drop was introduced in API 11. So you could try differentiating between versions:

if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {
    //MyDragEventListener comes in here
}

Otherwise as said, there is no official Drag&Drop for < API 11. There is not much use of implementing it for Android below API 14 (or API 15 for that matter), because per Android Studio, there are only a handful of devices running below that version, i.e. only mere 2.3%.

If you still, insist on doing it, you could use a third party library such as Android-DragArea.

Hope this helps!



来源:https://stackoverflow.com/questions/39596889/startdrag-method-deprecated-and-unable-to-compile-the-program

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!