如何通过Android拖放中的Clipdata发送和接收多个数据和数据类型?

时间:2017-01-13 07:44:14

标签: android drag-and-drop

此代码用于通过拖放操作通过clipdata发送和接收一个数据。

发送

ClipData.Item item = new ClipData.Item((CharSequence)number);

String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN};
ClipData dragData = new ClipData("",mimeTypes, item); 
  View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(adImage);
                adImage.startDrag(dragData, shadowBuilder, adImage, 0);

接收数据

ClipData.Item item = event.getClipData().getItemAt(0);

请告诉我如何通过拖放android中的clipdata发送多个数据。

1 个答案:

答案 0 :(得分:0)

我再次得到自己的回答。 如果你想用Clipdata发送多个数据,请这样做。

如果要通过Clipdata发送数据,则需要在item中发送数据。

发送数据:

String number="152468465";
Int a=55;

ClipData.Item item = new ClipData.Item((CharSequence)number);  // 1st item
ClipData.Item item1 = new ClipData.Item((CharSequence)a);     //2nd item and so on

String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN}; //Declare mime type according to your logic
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(adImage);
            adImage.startDrag(dragData, shadowBuilder, adImage, 1);// Here 1 is array index of Clipdata ;if you have one item then make it 0 and if you have more than one item then make it according to the array index .

RECEIVE:

ClipData.Item item = event.getClipData().getItemAt(0); //number
ClipData.Item item1 = event.getClipData().getItemAt(1);  //a