Drag and Drop Android API
2015-06-30 15:50
423 查看
在本教程中,我们将了解有关安卓的应用程序拖放API,以及如何使用它来创建一个示例应用程序。ontouchlistener和ondraglistener是实现拖放的关键接口,这些回调接口主要使用如下方法onlongpress和onTouch。
Design Layout: Design and Android layout that contains two or more bounding spaces and the view to be dragged.
Create Activity: Create activity which implements required Android listener interfaces.
Define Callbacks: Define the drag and drop call back functions onTouch() and onDrag().
Create Android views to be dragged between available bounding spaces.
The bounding space is nothing but any view like Linear layout, Relative layout, listview or like such views, which will contain the view for drag and drop. When the user touches this view, the drag event will be triggered.
We can even use OnLongClickListener, if so then the respective method to be implemented is onLongClick. For onTouch event, MotionEvent object and the View object should be sent as arguments. For onDr
4000
ag event the DragEvent object and View object should be passed.
When the user presses the View to be dragged, the methods onTouch() and onLongClick() will be invoked by the Android runtime. We should have our application
logic inside the method and that will executed on drag callback.
Clip data should be created and it should have data to be dragged and clip description. This clip data can be accessed on leaving the drag control using getClipData(). This clip data is optional. If the use case requires to pass information, then this can be
used. If created, the data will be sent via the startDrag() method. On invoking the startDrag(), the application will intimate the system about the start of the drag.
Before calling startDrag a
After the start of the drag is intimated to Android runtime, immediately it will allow the DragEventListener to handle the drag event using the onDrag() method. It holds two arguments as View and DragEvent object. On handling the drag event there are several
possible actions. That action is returned by the method getAction(). Those actions have different states and they are listed as follows.
ACTION_DRAG_STARTED – This action will be returned after the startDrag method is invoked.
ACTION_DRAG_ENTERED – Whenever the Android View to be dragged enters into a new bounding space, this action will be returned.
ACTION_DRAG_LOCATION – This will be returned for each touch point during the drag. It holds the x,y coordinates of the drag position.
ACTION_DRAG_EXITED – Once the dragged view leave some bounding space or layout, then this action will be returned.
ACTION_DROP – Once the finger release the hold from drag, then this is returned.
ACTION_DRAG_ENDED – This action is the end of the Android drag and drop cycle.
Code:
转载自:http://javapapers.com/android/android-drag-and-drop/
Steps for Drag and Drop Implementation
Design Layout: Design and Android layout that contains two or more bounding spaces and the view to be dragged.Create Activity: Create activity which implements required Android listener interfaces.
Define Callbacks: Define the drag and drop call back functions onTouch() and onDrag().
Step 1. Design Android Layout
Need to create two or more bounding spaces.Create Android views to be dragged between available bounding spaces.
The bounding space is nothing but any view like Linear layout, Relative layout, listview or like such views, which will contain the view for drag and drop. When the user touches this view, the drag event will be triggered.
Step 2. Create Android Activity
An Android activity class should be created and let it to implement the required drag and drop listener classes. These interfaces contains the drag and drop call back functions that will be called by the runtime when the event occurs.Step 3. Define Drag and Drop Callbacks
OnTouchListenerand
OnDragListenerare the two interfaces that needs to be implemented for drag and drop callback. These two Android interfaces contain one method each, respectively
onTouchand
onDrag.
We can even use OnLongClickListener, if so then the respective method to be implemented is onLongClick. For onTouch event, MotionEvent object and the View object should be sent as arguments. For onDr
4000
ag event the DragEvent object and View object should be passed.
onTouch() Drag and Drop Callback
When the user presses the View to be dragged, the methods onTouch() and onLongClick() will be invoked by the Android runtime. We should have our applicationlogic inside the method and that will executed on drag callback.
How do we pass meta data onDrag?
Clip data should be created and it should have data to be dragged and clip description. This clip data can be accessed on leaving the drag control using getClipData(). This clip data is optional. If the use case requires to pass information, then this can beused. If created, the data will be sent via the startDrag() method. On invoking the startDrag(), the application will intimate the system about the start of the drag.
How do we show drag shadow?
Before calling startDrag a shadowBuilderobject should be created. Pass the view instance to
View.DragShadowBuilder(view)to show drag shadow.
onDrag() Drag and Drop Callback
After the start of the drag is intimated to Android runtime, immediately it will allow the DragEventListener to handle the drag event using the onDrag() method. It holds two arguments as View and DragEvent object. On handling the drag event there are severalpossible actions. That action is returned by the method getAction(). Those actions have different states and they are listed as follows.
ACTION_DRAG_STARTED – This action will be returned after the startDrag method is invoked.
ACTION_DRAG_ENTERED – Whenever the Android View to be dragged enters into a new bounding space, this action will be returned.
ACTION_DRAG_LOCATION – This will be returned for each touch point during the drag. It holds the x,y coordinates of the drag position.
ACTION_DRAG_EXITED – Once the dragged view leave some bounding space or layout, then this action will be returned.
ACTION_DROP – Once the finger release the hold from drag, then this is returned.
ACTION_DRAG_ENDED – This action is the end of the Android drag and drop cycle.
Code:
activity_main.xml
<RelativeLayout xmlns:tools="http://schemas.android.com/tools" xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/center" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <LinearLayout android:id="@+id/pinkLayout" android:layout_width="fill_parent" android:layout_height="210dp" android:background="#FF8989" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/dragtext" /> </LinearLayout> <LinearLayout android:id="@+id/yellowLayout" android:layout_width="fill_parent" android:layout_height="250dp" android:layout_marginTop="210dp" android:background="#FFCC00" android:orientation="vertical" > </LinearLayout> </RelativeLayout>
package com.javapapers.android.drag_drop; import android.os.Bundle; import android.app.Activity; import android.view.*; import android.view.View.OnDragListener; import android.view.View.OnTouchListener; import android.view.View.DragShadowBuilder; import android.widget.LinearLayout; import android.util.Log; import com.javapapers.android.drag_drop.R; public class MainActivity extends Activity implements OnTouchListener,OnDragListener{ private static final String LOGCAT = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); findViewById(R.id.textView1).setOnTouchListener(this); findViewById(R.id.pinkLayout).setOnDragListener(this); findViewById(R.id.yellowLayout).setOnDragListener(this); } public boolean onTouch(View view, MotionEvent motionEvent) { if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) { DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view); view.startDrag(null, shadowBuilder, view, 0); view.setVisibility(View.INVISIBLE); return true; } else { return false; } } public boolean onDrag(View layoutview, DragEvent dragevent) { int action = dragevent.getAction(); switch (action) { case DragEvent.ACTION_DRAG_STARTED: Log.d(LOGCAT, "Drag event started"); break; case DragEvent.ACTION_DRAG_ENTERED: Log.d(LOGCAT, "Drag event entered into "+layoutview.toString()); break; case DragEvent.ACTION_DRAG_EXITED: Log.d(LOGCAT, "Drag event exited from "+layoutview.toString()); break; case DragEvent.ACTION_DROP: Log.d(LOGCAT, "Dropped"); View view = (View) dragevent.getLocalState(); ViewGroup owner = (ViewGroup) view.getParent(); owner.removeView(view); LinearLayout container = (LinearLayout) layoutview; container.addView(view); view.setVisibility(View.VISIBLE); break; case DragEvent.ACTION_DRAG_ENDED: Log.d(LOGCAT, "Drag ended"); break; default: break; } return true; } }
转载自:http://javapapers.com/android/android-drag-and-drop/
相关文章推荐
- 使用C++实现JNI接口需要注意的事项
- Android IPC进程间通讯机制
- Android Manifest 用法
- [转载]Activity中ConfigChanges属性的用法
- Android之获取手机上的图片和视频缩略图thumbnails
- Android之使用Http协议实现文件上传功能
- Android学习笔记(二九):嵌入浏览器
- android string.xml文件中的整型和string型代替
- i-jetty环境搭配与编译
- android之定时器AlarmManager
- android wifi 无线调试
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- android 代码实现控件之间的间距
- android FragmentPagerAdapter的“标准”配置
- Android"解决"onTouch和onClick的冲突问题
- android:installLocation简析
- android searchView的关闭事件
- SourceProvider.getJniDirectories