【开源】DragNDropList
DragNDropList
- https://github.com/terlici/DragNDropList
介绍:
带拖动排序功能的ListView。比起DragSortListView来这个要简陋很多,不过可以从这个项目上学到drag功能的最基本实现方法。适合学习。运行效果:
![]()
使用说明:
xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.terlici.dragndroplist.DragNDropListView
android:id="@id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
item 布局
DragNDropListView的每个item都有一个处理拖动的drag handler。下面是一个普通的item布局,他包含了一个星星图像的ImageView以及一个textView,用户触摸星星的时候就可以开始拖动排序。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="80px"
android:orientation="horizontal"
android:gravity="center_vertical"
>
<ImageView
android:id="@+id/handler"
android:layout_width="60px"
android:layout_height="60px"
android:src="@android:drawable/btn_star_big_on"
android:layout_marginLeft="8px"
/>
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
adapter的设置
普通的ListView是如下设置adapter的:
ListView list = (ListView)findViewById(android.R.id.list);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(context,
R.layout.row,
cursor,
new String[]{"text"},
new int[]{R.id.text},
0);
list.setAdapter(adapter);
而在DragNDropListView中SimpleCursorAdapter需要替换成DragNDropCursorAdapter同时将draghandler的id作为DragNDropCursorAdapter的最后一个参数。在上面的布局中这参数的值就是R.id.handler。最后setAdapter替换成setDragNDropAdapter。
DragNDropListView list = (DragNDropListView)findViewById(android.R.id.list);
DragNDropCursorAdapter adapter = new DragNDropCursorAdapter(context,
R.layout.row,
cursor,
new String[]{"text"},
new int[]{R.id.text},
R.id.handler);
list.setDragNDropAdapter(adapter);
事件
DragNDropListView为拖动提供了监听者
list.setOnItemDragNDropListener(...)
该监听有如下接口
public interface OnItemDragNDropListener {
// Called when the item begins dragging.
public void onItemDrag(DragNDropListView parent, View view, int position, long id);
// Called after the item is dropped in place
public void onItemDrop(DragNDropListView parent, View view, int startPosition, int endPosition, long id);
}
demo
posted on 2015-04-03 10:36 wasdchenhao 阅读(194) 评论(0) 收藏 举报

浙公网安备 33010602011771号