android交互功能,Android 用户界面交互---拖放(OnDragListener)

本文详细介绍了在Android中实现拖放功能的步骤,包括如何开始拖拽、响应拖拽事件、处理落下事件和结束拖放操作。内容涵盖创建ClipData、设置拖拽影子、监听拖放事件及处理ACTION_DRAG_ENTERED、ACTION_DRAG_LOCATION、ACTION_DRAG_EXITED和ACTION_DROP事件。
摘要由CSDN通过智能技术生成

设计拖放操作

本节主要内容如下:

1.  如何开始拖拽;

2.  在拖拽期间如何响应事件;

3.  如何响应落下事件;

4.  如何结束拖放操作。

开始拖拽

用户使用一个拖拽手势开始拖拽,通常是在View对象上长按。在响应中,应该做下列事情:

1.  必要时,给要移动的数据创建一个ClipData和ClipData.Item对象,作为ClipData对象的一部分,在ClipData对象内部的ClipDescription对象中保存了元数据。因为拖放操作不代表数据的移动,因此可以使用null来代替实际的对象。

例如,以下代码段显示了如何在ImageView对象的长按事件上创建一个包含ImageView对象标签的ClipData对象。// Create a string for the ImageView label

private static final String IMAGEVIEW_TAG = "icon bitmap"

// Creates a new ImageView

ImageView imageView = new ImageView(this);

// Sets the bitmap for the ImageView from an icon bit map (defined elsewhere)

imageView.setImageBitmap(mIconBitmap);

// Sets the tag

imageView.setTag(IMAGEVIEW_TAG);

...

// Sets a long click listener for the ImageView using an anonymous listener object that

// implements the OnLongClickListener interface

imageView.setOnLongClickListener(new View.OnLongClickListener() {

// Defines the one method for the interface, which is called when the View is long-clicked

public boolean onLongClick(View v) {

// Create a new ClipData.

// This is done in two steps to provide clarity. The convenience method

// ClipData.newPlainText() can create a plain text ClipData in one step.

// Create a new ClipData.Item from the ImageView object's tag

ClipData.Item item = new ClipData.Item(v.getTag());

// Create a new ClipData using the tag as a label, the plain text MIME type, and

// the already-created item. This will create a new ClipDescription object within the

// ClipData, and set its MIME type entry to "text/plain"

ClipData dragData = new ClipData(v.getTag(),ClipData.MIMETYPE_TEXT_PLAIN,item);

// Instantiates the drag shadow builder.

View.DragShadowBuilder myShadow = new MyDragShadowBuilder(imageView);

// Starts the drag

v.startDrag(dragData,  // the data to be dragged

myShadow,  //

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值