1. 概述:
HoneyComb3.0支持View的拖放操作,可以将一个View从某一个位置拖放到另一个位置,在拖放过程中必须有两个或两个以上的View参与,一
个是被拖的View,另一个或多个是接收被拖View的View。
2. 效果图:
(1)屏幕中存在两个View(一个红色区域(被拖View),一个绿色区域(接收被拖View的View)),如下图:
(2)长按红色区域(被拖View)开始拖动,在开始拖动某一个View时屏幕中所有View的DragEvent.ACTION_DRAG_STARTED事件被触发,
如下图:
(3)拖动红色区域(被拖View)进入绿色区域(接收被拖View的View),进入绿色区域后会触发绿色View的
DragEvent.ACTION_DRAG_ENTERED,DragEvent.ACTION_DRAG_LOCATION等事件,如下图。
(4)将红色区域拖到绿色区域后松开鼠标(Drop操作),这时会触发绿色View的DragEvent.ACTION_DROP,
DragEvent.ACTION_DRAG_ENDED等事件,红绿区域交换位置,如下图:
3. 代码实现:
(1)自定义第一个View,即:被拖View,命名:AreaOne.java,代码:
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
public class AreaOne extends View {
public AreaOne(Context context) {
super(context);
}
public AreaOne(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
(2)自定义第二个View,即:接受被拖View的View,实现onDragEvent()方法,命名:AreaTwo.java,代码:
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.DragEvent;
import android.view.View;
public class AreaTwo extends View {
public AreaTwo(Context context) {
super(context);
}
public AreaTwo(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onDragEvent(DragEvent event) {
boolean result = false;
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED: {
Log.i("mayingcai", "有View开始被拖动!");
/**
* 在拖动开始时,只有返回true,后面的动作(ACTION_DRAG_ENTERED, ACTION_DRAG_LOCATION, ACTION_DROP)才会被执行!
*/
result = true;
break;
}
case DragEvent.ACTION_DRAG_ENTERED: {
Log.i("mayingcai", "被拖动的View进入当前View!");
break;
}
case DragEvent.ACTION_DRAG_LOCATION: {
Log.i("mayingcai", "被拖动的View进入当前View后,位置发生改变!");
break;
}
case DragEvent.ACTION_DROP: {
Log.i("mayingcai", "拖动的View被放入当前View!");
/**
* 在放时交互两个View的背景。
*/
View mDragView = (View) event.getLocalState();
Drawable mDragViewBackgroud = mDragView.getBackground();
mDragView.setBackgroundDrawable(this.getBackground());
this.setBackgroundDrawable(mDragViewBackgroud);
break;
}
case DragEvent.ACTION_DRAG_ENDED: {
Log.i("mayingcai", "拖动结束!");
break;
}
case DragEvent.ACTION_DRAG_EXITED: {
Log.i("mayingcai", "拖动退出!");
break;
}
default: {
break;
}
}
return result;
}
}
(3)主布局(main.xml)实现:
<LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android"
android:orientation = "vertical"
android:layout_width = "fill_parent"
android:layout_height = "fill_parent"
>
<com.focus.AreaOne
android:id = "@+id/areaOne"
android:layout_width = "100dip"
android:layout_height = "100dip"
android:background = "#FF0000"
/>
<TextView
android:layout_width = "100dip"
android:layout_height = "100dip"
/>
<com.focus.AreaTwo
android:id = "@+id/areaTwo"
android:layout_width = "100dip"
android:layout_height = "100dip"
android:background = "#00FF00"
/>
</LinearLayout>
(4)主Activity实现:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnLongClickListener;
public class DragAndDropActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final View mAreaOne = findViewById(R.id.areaOne);
mAreaOne.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View view) {
/**
* 长按AreaOne后开始拖动。
*/
mAreaOne.startDrag(null, new DragShadowBuilder(view), (Object)view, 0);
return true;
}
});
}
}