Android控件中ListView左右滑动删除以及手动排序是一个很炫也很使用的功能,网上虽有国内大牛实现该功能,但应用起来总感觉滑动不是很流畅,容易报错,在gitHub上有个老外写的挺好的:点击打开链接。
这里做一个简单的使用说明,下载完drag-sort-listview后,把library导入项目中,然后再添加到你需要使用该listview的项目中。
在XML布局文件中:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<com.mobeta.android.dslv.DragSortListView
xmlns:dslv="http://schemas.android.com/apk/res/com.test.androidtest"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="5dp"
android:dividerHeight="5dp"
android:fastScrollEnabled="true"
dslv:collapsed_height="2dp"
dslv:drag_enabled="true"
dslv:drag_handle_id="@id/drag_handle"
dslv:drag_scroll_start="0.99"
dslv:drag_start_mode="onDown"
dslv:float_alpha="0.6"
dslv:max_drag_scroll_speed="0.6"
dslv:remove_enabled="true"
dslv:remove_mode="flingRemove"
dslv:slide_shuffle_speed="0.5"
dslv:sort_enabled="true"
dslv:track_drag_sort="false"
dslv:use_default_controller="true" />
</LinearLayout>
package com.test.androidtest;
import java.util.ArrayList;
import java.util.Arrays;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import com.mobeta.android.dslv.DragSortListView;
public class MainActivity extends ListActivity {
private ArrayAdapter<String> adapter;
private String[] array;
private ArrayList<String> list;
private DragSortListView.DropListener onDrop = new DragSortListView.DropListener() {
@Override
public void drop(int from, int to) {
String item = adapter.getItem(from);
adapter.notifyDataSetChanged();
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));
}
};
private DragSortListView.DragScrollProfile ssProfile = new DragSortListView.DragScrollProfile() {
@Override
public float getSpeed(float w, long t) {
if (w > 0.8f) {
// Traverse all views in a millisecond
return ((float) adapter.getCount()) / 0.001f;
} else {
return 10.0f * w;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DragSortListView lv = (DragSortListView) getListView();
lv.setDropListener(onDrop);
lv.setRemoveListener(onRemove);
lv.setDragScrollProfile(ssProfile);
array = getResources().getStringArray(R.array.countries);
list = new ArrayList<String>(Arrays.asList(array));
adapter = new ArrayAdapter<String>(this,
R.layout.list_item_handle_right, R.id.text, list);
setListAdapter(adapter);
}
}
list_item_handle_right.xml的布局为:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="@dimen/item_height"
android:orientation="horizontal">
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="@dimen/item_height"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="center_vertical"
android:paddingLeft="8dp" />
<!-- <ImageView
android:id="@id/drag_handle"
android:background="@drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="@dimen/item_height"
android:layout_weight="0" /> -->
</LinearLayout>
ImageView注释掉就可以不滑动了。
解释的很粗糙,希望能有抛砖引玉的作用