Last active
May 7, 2019 18:18
-
-
Save Viyu/8093281 to your computer and use it in GitHub Desktop.
Android Drag And Drop Demo
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" | |
android:id="@+id/container" | |
android:layout_width="fill_parent" | |
android:layout_height="fill_parent" | |
android:orientation="vertical" > | |
<TextView | |
android:id="@+id/main_note_1" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentTop="true" | |
android:layout_marginTop="20dip" | |
android:layout_centerHorizontal="true" | |
android:background="@color/note_bg" | |
android:text="@string/note1" | |
android:textSize="24sp" | |
android:padding="10dip" /> | |
<TextView | |
android:id="@+id/main_note_2" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_below="@id/main_note_1" | |
android:layout_marginTop="20dip" | |
android:layout_centerHorizontal="true" | |
android:background="@color/note_bg" | |
android:text="@string/note2" | |
android:textSize="24sp" | |
android:padding="10dip" /> | |
<ImageView | |
android:id="@+id/main_trash" | |
android:layout_width="wrap_content" | |
android:layout_height="wrap_content" | |
android:layout_alignParentBottom="true" | |
android:layout_centerHorizontal="true" | |
android:layout_marginBottom="20dip" | |
android:src="@drawable/trash" /> | |
</RelativeLayout> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.viyu.draganddropdemo; | |
import com.viyu.dradanddropdemo.R; | |
import android.app.Activity; | |
import android.content.ClipData; | |
import android.content.ClipDescription; | |
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.widget.ImageView; | |
import android.widget.TextView; | |
import android.widget.Toast; | |
public class MainActivity extends Activity{ | |
private TextView mNote_1 = null; | |
private TextView mNote_2 = null; | |
private ImageView trashView = null; | |
private static final String TAG_NOTE_1 = "Note_1"; | |
private static final String TAG_NOTE_2 = "Note_2"; | |
@Override | |
public void onCreate(Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
trashView = (ImageView)findViewById(R.id.main_trash); | |
mNote_1 = (TextView)findViewById(R.id.main_note_1); | |
mNote_1.setTag(TAG_NOTE_1); | |
mNote_1.setOnLongClickListener(new View.OnLongClickListener() { | |
@Override | |
public boolean onLongClick(View v) { | |
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag()); | |
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN}; | |
ClipData dragData = new ClipData(v.getTag().toString(), | |
mimeTypes, item); | |
View.DragShadowBuilder myShadow = new DragShadowBuilder(mNote_1); | |
v.startDrag(dragData, myShadow, null, 0); | |
return true; | |
} | |
}); | |
mNote_2 = (TextView)findViewById(R.id.main_note_2); | |
mNote_2.setTag(TAG_NOTE_2); | |
mNote_2.setOnLongClickListener(new View.OnLongClickListener() { | |
@Override | |
public boolean onLongClick(View v) { | |
ClipData.Item item = new ClipData.Item((CharSequence)v.getTag()); | |
String[] mimeTypes = {ClipDescription.MIMETYPE_TEXT_PLAIN}; | |
ClipData dragData = new ClipData(v.getTag().toString(), mimeTypes, item); | |
View.DragShadowBuilder myShadow = new DragShadowBuilder(mNote_2); | |
v.startDrag(dragData, myShadow, null, 0); | |
return true; | |
} | |
}); | |
trashView.setOnDragListener( new OnDragListener(){ | |
@Override | |
public boolean onDrag(View v, DragEvent event){ | |
switch(event.getAction()) | |
{ | |
case DragEvent.ACTION_DROP: | |
Log.d("trash view", "ACTION_DROP event"); | |
ClipData data = event.getClipData(); | |
CharSequence noteName = data.getItemAt(0).getText(); | |
if(noteName.equals(TAG_NOTE_1)) { | |
mNote_1.setVisibility(View.GONE); | |
Toast.makeText(MainActivity.this, "Note 1 has been removed.", Toast.LENGTH_SHORT).show(); | |
} else if(noteName.equals(TAG_NOTE_2)) { | |
Toast.makeText(MainActivity.this, "Note 2 has been removed.", Toast.LENGTH_SHORT).show(); | |
mNote_2.setVisibility(View.GONE); | |
} | |
break; | |
default: break; | |
} | |
return true; | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment