1. 概述:
接收被拖View的View实现有两种方式,第一种:接收View实现onDragEvent()方法,第二种:接收View添加OnDragListener监听器。
第一种实现方法已经在 HoneyComb3.0技术系列之Drag And Drop(一) 文章中讲过,下面讲第二种实现方法。
2. 效果图:
与 HoneyComb3.0技术系列之Drag And Drop(一) 文章中的效果相同。
3. 代码实现:
(1)自定义第一个View,即:被拖View,命名:AreaOne.java,代码:package com.focus; 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,命名:AreaTwo.java,代码:package com.focus; import android.content.Context; import android.util.AttributeSet; import android.view.View; public class AreaTwo extends View { public AreaTwo(Context context) { super(context); } public AreaTwo(Context context, AttributeSet attrs) { super(context, attrs); } }
(3)主布局(main.xml)实现:<?xml version="1.0" encoding="utf-8"?> <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实现:package com.focus; import android.app.Activity; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.util.Log; import android.view.DragEvent; import android.view.View; import android.view.View.DragShadowBuilder; import android.view.View.OnDragListener; 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; } }); /** * 结AreaTwo添加Drag监听器。 */ final View mAreaTwo = findViewById(R.id.areaTwo); mAreaTwo.setOnDragListener(new OnDragListener() { public boolean onDrag(View mDropView, 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(mDropView.getBackground()); mDropView.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; } }); } }