开源drag-sort-listview实现左右滑动删除和排序

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>

其中com.test.androidtest资源路径换成你自己的项目包路径(在AndroidManifest.xml文件中可以看到)。Activity的实现如下:

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注释掉就可以不滑动了。

解释的很粗糙,希望能有抛砖引玉的作用


  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值