Drag and Drop items between ListView

This example show how to move items between ListView by Drag and Drop. (I don't know any standard method to achieve this, it's my own implementation)


MainActivity.java
package com.example.androidimageviewlist;

import java.util.ArrayList;
import java.util.List;

import android.support.v7.app.ActionBarActivity;
import android.text.method.ScrollingMovementMethod;
import android.app.Activity;
import android.content.ClipData;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.DragEvent;
import android.view.LayoutInflater;
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.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity {

//items stored in ListView
public class Item {
Drawable ItemDrawable;
String ItemString;
Item(Drawable drawable, String t){
ItemDrawable = drawable;
ItemString = t;
}
}

//objects passed in Drag and Drop operation
class PassObject{
View view;
Item item;

PassObject(View v, Item i){
view = v;
item = i;
}
}

static class ViewHolder {
ImageView icon;
TextView text;
}

public class ItemsListAdapter extends BaseAdapter {

private Context context;
private List<Item> list;

ItemsListAdapter(Context c, List<Item> l){
context = c;
list = l;
}

@Override
public int getCount() {
return list.size();
}

@Override
public Object getItem(int position) {
return list.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;

// reuse views
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater.inflate(R.layout.row, null);

ViewHolder viewHolder = new ViewHolder();
viewHolder.icon = (ImageView) rowView.findViewById(R.id.rowImageView);
viewHolder.text = (TextView) rowView.findViewById(R.id.rowTextView);
rowView.setTag(viewHolder);
}

ViewHolder holder = (ViewHolder) rowView.getTag();
holder.icon.setImageDrawable(list.get(position).ItemDrawable);
holder.text.setText(list.get(position).ItemString);

return rowView;
}

public List<Item> getList(){
return list;
}
}

List<Item> items1, items2;
ListView listView1, listView2;
ItemsListAdapter myItemsListAdapter1, myItemsListAdapter2;
LinearLayout area1, area2;
TextView prompt;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView1 = (ListView)findViewById(R.id.listview1);
listView2 = (ListView)findViewById(R.id.listview2);

area1 = (LinearLayout)findViewById(R.id.pane1);
area2 = (LinearLayout)findViewById(R.id.pane2);
area1.setOnDragListener(myOnDragListener);
area2.setOnDragListener(myOnDragListener);

initItems();
myItemsListAdapter1 = new ItemsListAdapter(this, items1);
myItemsListAdapter2 = new ItemsListAdapter(this, items2);
listView1.setAdapter(myItemsListAdapter1);
listView2.setAdapter(myItemsListAdapter2);

//Auto scroll to end of ListView
listView1.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);
listView2.setTranscriptMode(ListView.TRANSCRIPT_MODE_ALWAYS_SCROLL);

listView1.setOnItemClickListener(listOnItemClickListener);
listView2.setOnItemClickListener(listOnItemClickListener);

listView1.setOnItemLongClickListener(myOnItemLongClickListener);
listView2.setOnItemLongClickListener(myOnItemLongClickListener);

prompt = (TextView) findViewById(R.id.prompt);
// make TextView scrollable
prompt.setMovementMethod(new ScrollingMovementMethod());
//clear prompt area if LongClick
prompt.setOnLongClickListener(new OnLongClickListener(){

@Override
public boolean onLongClick(View v) {
prompt.setText("");
return true;
}});

}
OnItemLongClickListener myOnItemLongClickListener = new OnItemLongClickListener(){

@Override
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Item selectedItem = (Item)(parent.getItemAtPosition(position));
PassObject passObj = new PassObject(view, selectedItem);


ClipData data = ClipData.newPlainText("", "");
DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(view);
view.startDrag(data, shadowBuilder, passObj, 0);

return true;
}

};

OnDragListener myOnDragListener = new OnDragListener() {

@Override
public boolean onDrag(View v, DragEvent event) {
String area;
if(v == area1){
area = "area1";
}else if(v == area2){
area = "area2";
}else{
area = "unknown";
}

switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
prompt.append("ACTION_DRAG_STARTED: " + area + "\n");
break;
case DragEvent.ACTION_DRAG_ENTERED:
prompt.append("ACTION_DRAG_ENTERED: " + area + "\n");
break;
case DragEvent.ACTION_DRAG_EXITED:
prompt.append("ACTION_DRAG_EXITED: " + area + "\n");
break;
case DragEvent.ACTION_DROP:
prompt.append("ACTION_DROP: " + area + "\n");

PassObject passObj = (PassObject)event.getLocalState();
View view = passObj.view;
Item passedItem = passObj.item;
ListView oldParent = (ListView)view.getParent();
LinearLayout newParent = (LinearLayout)v;

if(oldParent.equals(listView1)){
prompt.append("Drag source: listView1" + "\n");

if(newParent.equals(area2)){
prompt.append("Drop target: area2" + "\n");

if(removeItemToList(items1, passedItem)){
addItemToList(items2, passedItem);
}
myItemsListAdapter1.notifyDataSetChanged();
myItemsListAdapter2.notifyDataSetChanged();

}else{
prompt.append("Invalid Drop target" + "\n");
}

}else if(oldParent.equals(listView2)){
prompt.append("Drag source: listView2" + "\n");

if(newParent.equals(area1)){
prompt.append("Drop target: area1" + "\n");

if(removeItemToList(items2, passedItem)){
addItemToList(items1, passedItem);
}
myItemsListAdapter1.notifyDataSetChanged();
myItemsListAdapter2.notifyDataSetChanged();
}else{
prompt.append("Invalid Drop target" + "\n");
}

}else{
prompt.append("Invalid Drag source: unknown" + "\n");
}
break;
case DragEvent.ACTION_DRAG_ENDED:
prompt.append("ACTION_DRAG_ENDED: " + area + "\n");
default:
break;
}

return true;
}

};

OnItemClickListener listOnItemClickListener = new OnItemClickListener(){

@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(MainActivity.this,
((Item)(parent.getItemAtPosition(position))).ItemString,
Toast.LENGTH_SHORT).show();
}

};

private void initItems(){
items1 = new ArrayList<Item>();
items2 = new ArrayList<Item>();

TypedArray arrayDrawable = getResources().obtainTypedArray(R.array.resicon);
TypedArray arrayText = getResources().obtainTypedArray(R.array.restext);

for(int i=0; i<arrayDrawable.length(); i++){
Drawable d = arrayDrawable.getDrawable(i);
String s = arrayText.getString(i);
Item item = new Item(d, s);
items1.add(item);
}

arrayDrawable.recycle();
arrayText.recycle();
}

private boolean removeItemToList(List<Item> l, Item it){
boolean result = l.remove(it);
return result;
}

private boolean addItemToList(List<Item> l, Item it){
boolean result = l.add(it);
return result;
}

}

/res/layout/activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="4dp"
tools:context="com.example.androidimageviewlist.MainActivity" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:autoLink="web"
android:text="http://android-er.blogspot.com/"
android:textStyle="bold" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="@android:color/background_dark"
android:orientation="horizontal" >

<LinearLayout
android:id="@+id/pane1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="1"
android:background="@android:color/background_light"
android:orientation="vertical" >

<ListView
android:id="@+id/listview1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>

<LinearLayout
android:id="@+id/pane2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:layout_weight="1"
android:background="@android:color/background_light"
android:orientation="vertical" >

<ListView
android:id="@+id/listview2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >

<TextView
android:id="@+id/prompt"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:gravity="bottom"
android:textColor="@android:color/white" />
</LinearLayout>

</LinearLayout>

/res/layout/row.xml and /res/values/arrays.xml, refer to previous post of "Custom ListView with ImageView".

To use Drag and Drop on your app, you have to modify AndroidManifest.xml to set android:minSdkVersion="11".

download filesDownload the files.


Next:
- It's a too un-flexible approach, The source and destination of the Drag and Drop operation is hard coded and fixed. Next is a "Improved Drag and Drop items between ListView".

- In the exercise "Drag and Drop items between ListView, with OnDragListener on ListView cells", another OnDragListener (ItemOnDragListener) implemented for rowView of ListView items, such that we can actually drop on ListView items, and insert in the previous position.