这是一个简短的程序,介绍如何使用我刚刚设法自己编写的库。基本上,它与样品是一样的,只是在一个地方。
package com.example.dndlist;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.ArrayAdapter;
import com.mobeta.android.dslv.DragSortController;
import com.mobeta.android.dslv.DragSortListView;
public class MainActivity extends Activity
{
DragSortListView listView;
ArrayAdapter<String> adapter;
private DragSortListView.DropListener onDrop = new DragSortListView.DropListener()
{
@Override
public void drop(int from, int to)
{
if (from != to)
{
String item = adapter.getItem(from);
adapter.remove(item);
adapter.insert(item, to);
}
}
};
private DragSortListView.RemoveListener onRemove = new DragSortListView.RemoveListener()
{
@Override
public void remove(int which)
{
adapter.remove(adapter.getItem(which));
}
};
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (DragSortListView) findViewById(R.id.listview);
String[] names = getResources().getStringArray(R.array.random_names);
ArrayList<String> list = new ArrayList<String>(Arrays.asList(names));
adapter = new ArrayAdapter<String>(this,
R.layout.item_layout, R.id.textView1, list);
listView.setAdapter(adapter);
listView.setDropListener(onDrop);
listView.setRemoveListener(onRemove);
DragSortController controller = new DragSortController(listView);
controller.setDragHandleId(R.id.imageView1);
//controller.setClickRemoveId(R.id.);
controller.setRemoveEnabled(false);
controller.setSortEnabled(true);
controller.setDragInitMode(1);
//controller.setRemoveMode(removeMode);
listView.setFloatViewManager(controller);
listView.setOnTouchListener(controller);
listView.setDragEnabled(true);
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:dslv="http://schemas.android.com/apk/res/com.example.dndlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<com.mobeta.android.dslv.DragSortListView
android:id="@+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:dividerHeight="5dp"
android:paddingBottom="0dp"
android:paddingLeft="10dp"
android:paddingTop="0dp"
dslv:collapsed_height="2dp"
dslv:drag_enabled="true"
dslv:drag_handle_id="@drawable/drag_handle"
dslv:drag_scroll_start="0.33"
dslv:drag_start_mode="onMove"
dslv:float_alpha="0.6"
dslv:max_drag_scroll_speed="0.5"
dslv:remove_enabled="true"
dslv:remove_mode="flingRemove"
dslv:slide_shuffle_speed="0.3"
dslv:sort_enabled="true"
dslv:track_drag_sort="false"
dslv:use_default_controller="true" />
</RelativeLayout>