1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105
| public class MainActivity extends ListActivity {
private ListView lvSort = null;
private ArrayAdapter<String> sortAdapter = null;
private View layoutDropArea = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] sortCategories = {"time", "service", "equipment", "type", "alert", "system", "priority"};
sortAdapter = new ArrayAdapter<String>(this, R.layout.list_item,R.id.textView1,
sortCategories);
setListAdapter(sortAdapter);
lvSort = getListView();
lvSort.setTextFilterEnabled(true);
layoutDropArea = findViewById(R.id.dropTarget);
setupDragDrop();
}
/**
* Setup what to do when we drag list items
*/
public void setupDragDrop() {
lvSort.setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int position, long arg3) {
TextView txtview = (TextView)v.findViewById(R.id.textView1);
String value = txtview.toString();
ClipData data = ClipData.newPlainText("procedure", value);
v.startDrag(data, new DragShadowBuilder(v), null, 0);//!!
return true;
}
});
DragListener mDragListener = new DragListener();
// mListView.setOnDragListener(mDragListener);
layoutDropArea.setOnDragListener(mDragListener);
}
private boolean processDrop(DragEvent event, int newPosition) {
ClipData data = event.getClipData();
if (data != null) {
if (data.getItemCount() > 0) {
Item item = data.getItemAt(0);
String value = item.toString();
updateViewsAfterDropComplete(value, newPosition);
return true;
}
}
return false;
}
private void updateViewsAfterDropComplete(String listItem, int index) {
Log.d("InsertItem", "Position: " + index);
sortAdapter.insert(listItem, index);
sortAdapter.notifyDataSetChanged();
}
private boolean processDragStarted(DragEvent event) {
ClipDescription clipDesc = event.getClipDescription();
if (clipDesc != null) {
return clipDesc.hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN);
}
return false;
}
protected class DragListener implements OnDragListener {
public boolean onDrag(View v, DragEvent event) {
final int action = event.getAction();
switch (action) {
case DragEvent.ACTION_DRAG_ENTERED:
// v.setBackgroundColor(Color.GRAY);
return false;
case DragEvent.ACTION_DRAG_EXITED:
// v.setBackgroundColor(Color.TRANSPARENT);
return true;
case DragEvent.ACTION_DRAG_STARTED:
return true;
case DragEvent.ACTION_DRAG_LOCATION:
// v.setVisibility(View.VISIBLE);
//return false;
return processDragStarted(event);
case DragEvent.ACTION_DROP:
v.setBackgroundColor(Color.TRANSPARENT);
int newPosition = lvSort.pointToPosition(
(int) (event.getX()), (int) event.getY());
Log.d("Position", Integer.toString(newPosition));
if (newPosition != ListView.INVALID_POSITION)
return processDrop(event, newPosition);
else
return false;
default:
return true;
}
}
}
} |
Partager