View imageView = new ImageView(this);
imageView.setOnDragListener(this);
// 以下為官方範例擷取
protected class myDragEventListener implements View.OnDragListener {
// This is the method that the system calls when it dispatches a drag event to the
// listener.
public boolean onDrag(View v, DragEvent event) {
// Defines a variable to store the action type for the incoming event
final int action = event.getAction();
switch(action) {
case DragEvent.ACTION_DRAG_STARTED:
// 圖形拔除
return false;
case DragEvent.ACTION_DRAG_ENTERED:
// 圖形貼上
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// 正在設置監聽的區塊內
return true;
case DragEvent.ACTION_DRAG_EXITED:
// 離開設置監聽的區塊內
return true;
case DragEvent.ACTION_DROP:
// 拖曳
return true;
case DragEvent.ACTION_DRAG_ENDED:
return true;
default:
break;
}
return false;
}
};
圖形解說 :
未整理
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/toplinear"
android:layout_width="fill_parent"
android:layout_height="170dp"
android:background="#aaaaaa"
android:layout_marginBottom="10dp" >
<ImageView
android:id="@+id/image_2"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_margin="4dip"
android:src="@drawable/m_2" />
<ImageView
android:id="@+id/image_3"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_margin="4dip"
android:src="@drawable/m_3" />
<ImageView
android:id="@+id/image_4"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_margin="4dip"
android:src="@drawable/m_4" />
</LinearLayout>
<LinearLayout
android:id="@+id/bottomlinear"
android:layout_width="fill_parent"
android:layout_height="170dp"
android:background="#555555"
android:layout_marginBottom="10dp" >
<TextView
android:id="@+id/text"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Drag the image and drop it here..." />
</LinearLayout>
<LinearLayout
android:id="@+id/bottomlinear_2"
android:layout_width="fill_parent"
android:layout_height="170dp"
android:background="#555555" >
<TextView
android:id="@+id/text_2"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="Drag the image and drop it here..." />
</LinearLayout>
</LinearLayout>
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.ClipData;
import android.content.ClipDescription;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.View;
import android.view.View.DragShadowBuilder;
import android.view.View.OnDragListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private List<String> listInfo;
private ImageView myImage_2;
private ImageView myImage_3;
private ImageView myImage_4;
private static final String IMAGEVIEW_TAG = "The Android Logo";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listInfo = new ArrayList<String>();
for(int i = 0; i<10; i++){
String a = null;
a = ""+ i%3;
listInfo.add(""+ a);
}
init();
setListener();
}
private void init(){
myImage_2 = (ImageView)findViewById(R.id.image_2);
myImage_3 = (ImageView)findViewById(R.id.image_3);
myImage_4 = (ImageView)findViewById(R.id.image_4);
// Sets the tag
myImage_2.setTag(IMAGEVIEW_TAG);
myImage_3.setTag(IMAGEVIEW_TAG);
myImage_4.setTag(IMAGEVIEW_TAG);
}
private void setListener(){
// set the listener to the dragging data
myImage_2.setOnLongClickListener(new MyClickListener());
myImage_3.setOnLongClickListener(new MyClickListener());
myImage_4.setOnLongClickListener(new MyClickListener());
findViewById(R.id.toplinear).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomlinear).setOnDragListener(new MyDragListener());
findViewById(R.id.bottomlinear_2).setOnDragListener(new MyDragListener());
}
private final class MyClickListener implements OnLongClickListener {
// called when the item is long-clicked
@Override
public boolean onLongClick(View view) {
// TODO Auto-generated method stub
// create it from the object's tag
ClipData.Item item = new ClipData.Item((CharSequence)view.getTag());
String[] mimeTypes = { ClipDescription.MIMETYPE_TEXT_PLAIN };
ClipData data = new ClipData(view.getTag().toString(), mimeTypes, item);
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag( data, //data to be dragged
shadowBuilder, //drag shadow
view, //local data about the drag and drop operation
0 //no needed flags
);
view.setVisibility(View.INVISIBLE);
return true;
}
}
class MyDragListener implements OnDragListener {
Drawable normalShape = getResources().getDrawable(R.drawable.normal_shape);
Drawable targetShape = getResources().getDrawable(R.drawable.target_shape);
@Override
public boolean onDrag(View v, DragEvent event) {
// Handles each of the expected events
switch (event.getAction()) {
//signal for the start of a drag and drop operation.
case DragEvent.ACTION_DRAG_STARTED:
// do nothing
break;
//the drag point has entered the bounding box of the View
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackground(targetShape); //change the shape of the view
break;
//the user has moved the drag shadow outside the bounding box of the View
case DragEvent.ACTION_DRAG_EXITED:
v.setBackground(normalShape); //change the shape of the view back to normal
break;
//drag shadow has been released,the drag point is within the bounding box of the View
case DragEvent.ACTION_DROP:
// if the view is the bottomlinear, we accept the drag item
if(v == findViewById(R.id.bottomlinear)) {
View view = (View) event.getLocalState();
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
//change the text
TextView text = (TextView) v.findViewById(R.id.text);
text.setText("The item is dropped");
LinearLayout containView = (LinearLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
} else if(v == findViewById(R.id.bottomlinear_2)) {
View view = (View) event.getLocalState();
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
//change the text
TextView text = (TextView) v.findViewById(R.id.text_2);
text.setText("The item is dropped");
LinearLayout containView = (LinearLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
} else if(v == findViewById(R.id.toplinear)) {
View view = (View) event.getLocalState();
ViewGroup viewgroup = (ViewGroup) view.getParent();
viewgroup.removeView(view);
LinearLayout containView = (LinearLayout) v;
containView.addView(view);
view.setVisibility(View.VISIBLE);
} else {
View view = (View) event.getLocalState();
view.setVisibility(View.VISIBLE);
Context context = getApplicationContext();
Toast.makeText(context, "You can't drop the image here",
Toast.LENGTH_LONG).show();
break;
}
break;
//the drag and drop operation has concluded.
case DragEvent.ACTION_DRAG_ENDED:
v.setBackground(normalShape); //go back to normal shape
default:
break;
}
return true;
}
}
參考 : 連結
參考 : CoolDragAndDrop
參考 : Using drag and drop in your application
參考 : Extending GridView with Drag and Drop for Grouping and Variable Sized Items
參考 : Drag and Drop between ListView and GridView
沒有留言:
張貼留言