Most visited

Recently visited

Added in API level 11

DragEvent

public class DragEvent
extends Object implements Parcelable

java.lang.Object
   ↳ android.view.DragEvent


表示系统在拖放操作期间不同时间发出的事件。 这是一个复杂的数据结构,它包含有关操作和底层数据的几个重要数据。

查看接收DragEvent调用getAction()对象,该对象返回指示拖放操作状态的操作类型。 这允许View对象通过改变其外观或执行其他操作来对状态变化做出反应。 例如,视图可以通过更改其显示图像中的一种或多种颜色来对ACTION_DRAG_ENTERED操作类型做出反应。

在拖放操作过程中,系统会显示用户拖动的图像。 这个图像被称为拖影。 几个动作类型反映了拖影相对于接收事件的视图的位置。

大多数方法仅为某些事件操作返回有效数据。 下表总结了这一点。 第一列中列出了每个可能的getAction()值。 其他列指示哪个或哪些方法返回该getAction()值的有效数据:

getAction() Value getClipDescription() getLocalState() getX() getY() getClipData() getResult()
ACTION_DRAG_STARTED X X X X    
ACTION_DRAG_ENTERED X X        
ACTION_DRAG_LOCATION X X X X    
ACTION_DRAG_EXITED X X        
ACTION_DROP X X X X X  
ACTION_DRAG_ENDED           X

getAction()describeContents()writeToParcel(Parcel, int) ,并 toString()方法总是返回有效的数据。

Developer Guides

有关实现拖放功能的指导,请阅读 Drag and Drop开发人员指南。

Summary

Constants

int ACTION_DRAG_ENDED

getAction()返回的操作常量:信号到视图,拖放操作已结束。

int ACTION_DRAG_ENTERED

getAction()返回的操作常量:信号到视图,拖动点已进入视图的边界框。

int ACTION_DRAG_EXITED

getAction()返回的操作常量:表示用户已将视图的边界框之外的拖动阴影移出。

int ACTION_DRAG_LOCATION

getAction()返回的动作常量:如果拖动阴影仍在View对象的边界框内,则发送到 ACTION_DRAG_ENTERED之后的视图。

int ACTION_DRAG_STARTED

getAction()返回的操作常量:表示拖放操作的开始。

int ACTION_DROP

getAction()返回的操作常量:向用户发出拖动阴影并且拖动点位于视图边界框内的视图的信号。

Inherited constants

From interface android.os.Parcelable

Fields

public static final Creator<DragEvent> CREATOR

用于从包创建DragEvent的容器。

Public methods

int describeContents()

返回有关此DragEvent对象的 Parcel表示形式的信息。

int getAction()

检查此事件的行为值。

ClipData getClipData()

作为对 startDrag()的调用的一部分,返回发送到系统的 ClipData对象。

ClipDescription getClipDescription()

作为对 startDrag()的调用的一部分,返回发送到系统的 ClipDescription对象中包含的 ClipData对象。

Object getLocalState()

作为对 startDrag()的调用的一部分,返回发送到系统的本地状态对象。

boolean getResult()

返回拖放操作结果的指示。

float getX()

获取拖动点的X坐标。

float getY()

获取拖动点的Y坐标。

String toString()

返回一个字符串,其中包含此DragEvent对象的简明易懂的表示形式。

void writeToParcel(Parcel dest, int flags)

从此DragEvent对象创建一个 Parcel对象。

Inherited methods

From class java.lang.Object
From interface android.os.Parcelable

Constants

ACTION_DRAG_ENDED

Added in API level 11
int ACTION_DRAG_ENDED

getAction()返回的操作常量:信号到视图,拖放操作已结束。 在操作过程中改变其外观的视图应该返回到其通常的绘图状态以响应此事件。

收到ACTION_DRAG_STARTED事件的所有视图都将收到ACTION_DRAG_ENDED事件,即使拖动结束时它们当前不可见也是如此。

View对象可以调用getResult()来查看操作的结果。 如果某个View返回true以响应ACTION_DROP ,则getResult()返回true ,否则返回false

也可以看看:

常量值:4(0x00000004)

ACTION_DRAG_ENTERED

Added in API level 11
int ACTION_DRAG_ENTERED

getAction()返回的操作常量:信号到视图,拖动点已进入视图的边界框。

如果视图可以接受放置,它可以通过改变它的外观来响应ACTION_DRAG_ENTERED,这种方式告诉用户View是当前放置目标。

The system stops sending ACTION_DRAG_LOCATION events to a View once the user moves the drag shadow out of the View object's bounding box. If the user moves the drag shadow back into the View object's bounding box, the View receives an ACTION_DRAG_ENTERED again before receiving any more ACTION_DRAG_LOCATION events.

也可以看看:

常量值:5(0x00000005)

ACTION_DRAG_EXITED

Added in API level 11
int ACTION_DRAG_EXITED

getAction()返回的操作常量:表示用户已将视图的边界框之外的拖动阴影移出。 View可以通过改变其外观来做出反应,告诉用户View不再是立即放置目标。

系统向视图发送ACTION_DRAG_EXITED事件后,视图将不会再收到ACTION_DRAG_LOCATION事件,直到用户将视图拖回到视图上。

常数值:6(0x00000006)

ACTION_DRAG_LOCATION

Added in API level 11
int ACTION_DRAG_LOCATION

getAction()返回的操作常量:如果拖动阴影仍在视图对象的边界框内,则发送到ACTION_DRAG_ENTERED之后的视图。 getX()getY()方法在View对象的边界框内提供拖动点的X和Y位置。

在收到任何ACTION_DRAG_LOCATION事件之前,View会收到一个 ACTION_DRAG_ENTERED事件。

一旦用户将拖动阴影移出视图对象的边界框,系统就停止向视图发送ACTION_DRAG_LOCATION事件。 如果用户将拖拽影移回到View对象的边界框中,则View在接收到更多ACTION_DRAG_LOCATION事件之前再次收到ACTION_DRAG_ENTERED。

也可以看看:

常量值:2(0x00000002)

ACTION_DRAG_STARTED

Added in API level 11
int ACTION_DRAG_STARTED

getAction()返回的操作常量:表示拖放操作的开始。 如果视图可以接受放置, true从其onDragEvent()处理程序方法或OnDragListener.onDrag()侦听器返回true onDragEvent()或onDrag()方法通常会检查来自getClipDescription()的元数据,以确定它们是否可以接受包含在此拖动中的数据。 对于不表示数据传输的操作,这些方法可能会执行其他操作来确定View是否接受拖动。 如果视图想要表明它是一个有效的放置目标,它也可以通过改变它的外观来作出反应。

如果视图返回 true以响应ACTION_DRAG_STARTED,View只会收到更多拖动事件。

也可以看看:

常数值:1(0x00000001)

ACTION_DROP

Added in API level 11
int ACTION_DROP

getAction()返回的操作常量:向用户发出拖动阴影并且拖动点位于视图边界框内的视图的信号。 视图应该通过调用getClipData()从DragEvent中检索数据。 方法getX()getY()返回视图对象边界框内的放置点的X和Y位置。

该视图应返回 true从其 onDragEvent(DragEvent)处理器或 OnDragListener.onDrag()监听器,如果它接受了下降, false如果忽略了下降。

视图也可以通过改变其外观来对此动作作出反应。

也可以看看:

常量值:3(0x00000003)

Fields

CREATOR

Added in API level 11
Creator<DragEvent> CREATOR

用于从包创建DragEvent的容器。

Public methods

describeContents

Added in API level 11
int describeContents ()

返回有关此DragEvent对象的 Parcel表示形式的信息。

Returns
int Information about the Parcel representation.

getAction

Added in API level 11
int getAction ()

检查此事件的行为值。

Returns
int One of the following action constants, in the order in which they usually occur during a drag and drop operation:

getClipData

Added in API level 11
ClipData getClipData ()

作为对startDrag()的调用的一部分,返回发送到系统的ClipData对象。 如果事件操作是ACTION_DROP此方法仅返回有效数据。

Returns
ClipData The ClipData sent to the system by startDrag().

getClipDescription

Added in API level 11
ClipDescription getClipDescription ()

作为对startDrag()的调用的一部分,返回发送到系统的ClipDescription对象中包含的ClipData对象。 View的拖动处理程序或侦听器可以使用此对象中的元数据来决定View是否可以接受拖动的View对象的数据。

此方法返回除 ACTION_DRAG_ENDED以外的所有事件操作的有效数据。

Returns
ClipDescription The ClipDescription that was part of the ClipData sent to the system by startDrag().

getLocalState

Added in API level 11
Object getLocalState ()

作为对startDrag()的调用的一部分,返回发送到系统的本地状态对象。 该对象旨在提供有关拖放操作的本地信息。 例如,它可以指示拖放操作是复制还是移动。

此方法返回除 ACTION_DRAG_ENDED之外的所有事件操作的有效数据。

Returns
Object The local state object sent to the system by startDrag().

getResult

Added in API level 11
boolean getResult ()

返回拖放操作结果的指示。 如果操作类型为ACTION_DRAG_ENDED此方法仅返回有效数据。 返回值取决于用户释放拖影后发生的情况。

如果用户在可接受放置的View上释放拖影,则系统会将ACTION_DROP事件发送到View对象的拖放事件侦听器。 如果监听器返回true ,那么getResult()将返回true 如果侦听器返回false ,则getResult()返回false

注意的getResult()也返回false如果没有ACTION_DROP发送。 例如,当用户在应用程序之外的区域释放拖影时会发生这种情况。 在这种情况下,系统发出ACTION_DRAG_ENDED当前操作,但从未发出ACTION_DROP

Returns
boolean true if a drag event listener returned true in response to ACTION_DROP. If the system did not send ACTION_DROP before ACTION_DRAG_ENDED, or if the listener returned false in response to ACTION_DROP, then false is returned.

getX

Added in API level 11
float getX ()

获取拖动点的X坐标。 如果事件动作值是唯一有效ACTION_DRAG_STARTEDACTION_DRAG_LOCATION或者ACTION_DROP

Returns
float The current drag point's X coordinate

getY

Added in API level 11
float getY ()

获取拖动点的Y坐标。 如果事件动作值是唯一有效ACTION_DRAG_STARTEDACTION_DRAG_LOCATION或者ACTION_DROP

Returns
float The current drag point's Y coordinate

toString

Added in API level 11
String toString ()

返回一个字符串,其中包含此DragEvent对象的简明易懂的表示形式。

Returns
String A string representation of the DragEvent object.

writeToParcel

Added in API level 11
void writeToParcel (Parcel dest, 
                int flags)

从此DragEvent对象创建一个 Parcel对象。

Parameters
dest Parcel: A Parcel object in which to put the DragEvent object.
flags int: Flags to store in the Parcel.

Hooray!