implement drag and drop on android
来源:互联网 发布:特效拍摄软件 编辑:程序博客网 时间:2024/03/29 15:25
Nowadays, drag and drop is a frequently seen feature on touch screen devices. Tthis post introduces the basic idea of how to implement drag and drop on android.
On android, touch event is composed of a series events.
First, user puts his finger on an element, and the element receives a ACTION_DOWN event.
Then, while holding finger on screen, user moves his finger to a new location. The element receives a series of ACTION_MOVE events.
Finally, user raises his finger. At this point, the element receives ACTION_UP event.
Consider the figure below, the parent element has a child element inside. User can drag the child element anywhere in parent.
There are two important traits of touch event on android.
First, touch event will be propagated. That is, if a child chooses to ignore the first event (ACTION_DOWN) by returning false in its onTouchEvent handler, the parent's onTouchEvent handler will receive the event. Unless one of the ancestors agrees to handle the event or the root is reached, the event will continually be propagated.
Second, parent can intercept the touch event before its child's onTouchEvent handler is fired. This is achieved by overriding the onInterceptTouchEvent method on parent, and returning true from it. As a consequence, the child's onTouchEvent handler will be bypassed, and the parent's onTouchEvent handler will fire.
The work flow is shown in below diagram:
We need to setup the onTouchEvent handler for both child and parent. In child's handler, we save the child element as the item to be dragged, and return false so that subsequent event will be delivered to parent's handler. In parent's handler, we change the child's margin to match the position of the finger, so the child will follow our finger.
Sample code:
http://code.google.com/p/rxwen-blog-stuff/source/browse/#svn/trunk/android/drag
Reference:
http://developer.android.com/guide/topics/ui/ui-events.html
On android, touch event is composed of a series events.
First, user puts his finger on an element, and the element receives a ACTION_DOWN event.
Then, while holding finger on screen, user moves his finger to a new location. The element receives a series of ACTION_MOVE events.
Finally, user raises his finger. At this point, the element receives ACTION_UP event.
Consider the figure below, the parent element has a child element inside. User can drag the child element anywhere in parent.
There are two important traits of touch event on android.
First, touch event will be propagated. That is, if a child chooses to ignore the first event (ACTION_DOWN) by returning false in its onTouchEvent handler, the parent's onTouchEvent handler will receive the event. Unless one of the ancestors agrees to handle the event or the root is reached, the event will continually be propagated.
Second, parent can intercept the touch event before its child's onTouchEvent handler is fired. This is achieved by overriding the onInterceptTouchEvent method on parent, and returning true from it. As a consequence, the child's onTouchEvent handler will be bypassed, and the parent's onTouchEvent handler will fire.
The work flow is shown in below diagram:
We need to setup the onTouchEvent handler for both child and parent. In child's handler, we save the child element as the item to be dragged, and return false so that subsequent event will be delivered to parent's handler. In parent's handler, we change the child's margin to match the position of the finger, so the child will follow our finger.
Sample code:
http://code.google.com/p/rxwen-blog-stuff/source/browse/#svn/trunk/android/drag
Reference:
http://developer.android.com/guide/topics/ui/ui-events.html
- implement drag and drop on android
- android - Drag and Drop
- Android Drag and Drop Tutorial
- Drag and Drop Android API
- Drag and Drop on a DataGridView
- Android 用户界面---拖放(Drag and Drop)
- Android SDK: Implementing Drag-and-Drop Functionality
- Android:Drag and Drop的应用
- Android翻译之---- Drag and Drop
- android drag and drop代码片段
- Android 用户界面---拖放(Drag and Drop)
- Android - Drag and Drop 可研究appdemos
- Android API Guides---Drag and Drop
- Drag and Drop
- 学习drag and drop
- 关于drag and drop
- html5 Drag and drop
- Ajax Drag and Drop
- 调整Linux终端显示分辨率
- poj 1258(最小生成树)
- 文件过滤驱动学习笔记(二)
- Ogre中设置渲染性能参数
- ubuntu10.04启用受限驱动后开机Splash分辨率变低的解决方案
- implement drag and drop on android
- nook's battery sucks
- 主函数 main WinMain _tmain _tWinMain 的区别
- why offsetof can use null pointer
- got nook
- Qt信号槽机制的简单例子
- 将ogre 嵌入到 Qt中
- 项目管理计划和实施建议
- 有点想用开发项目的思路,写点Ext3的入手文字