Android Drag and Drop getClipData returns always null

最后都变了- 提交于 2019-12-20 20:36:35

问题


I am designing a drag and drop operation but I don't know how to access my data. Has anyone experience with Clip Data objects? Here is my code:

Starting the drag and drop:

ClipData dragData= ClipData.newPlainText("my", "test") );
                    v.startDrag(dragData, 
                            new MyDragShadowBuilder(v),
                              v, 0);

Listening on the events:

case DragEvent.ACTION_DROP:{
    if (event.getClipDescription().getLabel().equals("my"))
           Log.d("myLog","Data:"+event.getClipData()+" "+event.getClipData().getItemCount());

回答1:


not in every drag event can get the clip data, but some of them, such as ACTION_DROP type

    dropableCanvas.setOnDragListener(new OnDragListener() {
        @Override
        public boolean onDrag(View v, DragEvent event) {
            switch (event.getAction()) {
            case DragEvent.ACTION_DRAG_STARTED:
                return true;
            case DragEvent.ACTION_DROP:
                ClipData clipData = event.getClipData();
                //...
                return true;
            default:
                return false;
            }
        }




回答2:


Before you start your drag set some clip data using the following code

ClipData.Item item = new ClipData.Item((CharSequence) v.getTag());
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item);

And then after you start dragging with v.startDrag(......); in the event DragEvent.ACTION_DROP you have to catch the clip data using the following code

String clipData = event.getClipDescription().getLabel().toString()

Once you have the clipData you can play around. This didn't return me null, check you at your end.



来源:https://stackoverflow.com/questions/6174897/android-drag-and-drop-getclipdata-returns-always-null

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