DragSortListView学习总结

来源:互联网 发布:php sql错误提示 编辑:程序博客网 时间:2024/04/30 07:54

Drag-sort-listview 是一个支持拖拽排序和左右滑动删除功能的自定义ListView,重写了 TouchInterceptor 类来提供更加优美的拖拽动画效果。

DSLV主要特性:

  • 完美的拖拽支持;

  • 在拖动时提供更平滑的滚动列表滚动;

  • 支持每个ListItem高度的多样性

  • 公开startDrag()和stopDrag()方法;

  • 有公开的接口可以自定义拖动的View


如何使用

有三个主要的元素来定义拖拽的操作。 初步地按照重要性排序如下:

  1. 数据重排. 拖拽排序重排ListView底层的数据顺序。由于DSLV 不知道您是如何组织您的数据的,所以重新组织数据必须由您自己通过实现相关的接口来实现。
  2. 开始/停止拖动. 通过调用DSLV的 startDrag() 和 stopDrag() 函数来启动或者停止拖动操作。 DragSortController这个助手类,提供了所有常用的 开始/停止/删除 拖拽操作功能。
  3. Floating View(拖动的View). 通过实现 FloatViewManager 接口可以控制 拖动的View 的视觉效果和行为。这样您可以显示任何内容作为 拖动的View,并且可以在拖动过程中更新其位置和显示状态。 DragSortController 助手类已经实现了该接口并提供了一些易用的实现方式。
第一条是必须的。如上所述 第二条和第三条可以通过 DragSortController 助手类实现。通过研究示例项目中的代码 可以更加深入的理解上述内容。

DragSortListView 可以和标准的View一样在XML布局文件中声明。
一些自定义XML属性说明:
  • collapsed_height: (dimension, 1px) 拖动起始位置占位符的高度。不能为0. Height of placeholder at original drag position. Cannot be zero.
  • drag_scroll_start: (float, 0.3) 拖动时开始滚动ListView的区域(为DSLV 高度的分数值,在0到1之前)Start of drag-scroll regions (defined by a fraction of the total DSLV height; i.e. between 0 and 1).
  • max_drag_scroll_speed: (float, 0.5) 默认线性加速的拖动时滚动的最大速度。单位:像素/毫秒。Maximum drag-scroll speed for default linear drag-scroll profile. Units of pixels/millisecond.

Listeners

DragSortListView 是个 ListView,因此需要一个 ListAdapter 来提供每个List Item。拖动排序是通过DSLV中的一些接口添加到ListAdapter的条目中的。可以有两种方式来设置DSLV 的各种接口:

  1. 通过set*Listener() 函数来设置
  2. 让自定义的 ListAdapter 实现这些接口,当调用 DragSortListView.setAdapter()函数时候,会检测实现的接口并设置 set*Listener()。

下面来描述每个接口:

DragSortListView.DropListener

DropListener接口有一个回调函数:

public void drop(int from, int to);

该函数在拖动排序完成的时候调用;例如 拖动View被放下了。参数 from 是ListView开始拖动的位置,to 是被放下的位置。该接口非常重要,否则DSLV无法完成拖动操作。

在 DSLV 上适当操作后,该函数需要重新排序ListAdapter中的数据。 For example, one often has a Cursor that pulls from a database and backs a CursorAdapter. The order of items in the Cursor is fixed; therefore, given drag-sorting, you must implement a mapping from Cursor positions to DSLV positions. This is commonly done within in a custom ListAdapter or CursorWrapper that implements the DropListener interface. See Issue #20 for a discussion of this. Update:  可以参考 DragSortCursorAdapter 类,该类对CursorAdapter提供了排序的支持!

如果 DSLV 对象的 android:choiceMode 不是 "none",并且您的 ListAdapter 没有稳定的ID,您则必须在 drop(from, to)中调用 DragSortListView.moveCheckState(int from, int to)。查看DSLV API 文档获取更多信息。

DragSortListView.RemoveListener

和 TI 做法一样,DSLV 提供了手势来删除拖动的View。 完成一个删除手势后,DSLV 调用RemoveListener 接口的函数:

public void remove(int which);

在ListAdapter中位置为 which 的条目需要被删除。是否真的删除或者只是标记删除由您自己实现。

在拖动操作之外您也可以删除条目。在任何时候都可以调用 DragSortListView.removeItem(int position) .

如果 DSLV 对象的 android:choiceMode 不是 "none",并且您的 ListAdapter 没有稳定的ID,您则必须在 remove(which)中调用 DragSortListView.moveCheckState(int from, int to)。查看DSLV API 文档获取更多信息。

DragSortListView.DragListener

DragListener 中的回调接口为

public void drag(int from, int to);

当拖动的View位于一个新条目上方的时候 会调用该函数。 to 是当前潜在的放下位置, from是前一个位置。  TI 实现中提供了这个接口,目前还没使用场景举例。

DragSortListView.DragSortListener

这只是一个继承了上面3个接口的便利接口。

FloatViewManager

该接口用来创建、更新和销毁拖动的View。通过函数 setFloatViewManager() 来设置。 SimpleFloatViewManager演示了其用法,该类是个简单的实现。用拖动List Item的快照实现一个拖动的View。

如果您需要实现不同的拖动View则可以提供自己的FloatViewManager。 在 onCreateFloatView() 函数中,确保返回的View有个固定的尺寸 (不要使用 MATCH_PARENT! 虽然对于宽度来说用 MATCH_PARENT 也是可以的). DSLV 会根据您拖动View上的ViewGroup.LayoutParams 参数来测量该View。如果没有LayoutParams参数则会使用 WRAP_CONTENT 和 MATCH_PARENT 来测量器高度和宽度。

Drag start/stop

在DragSortListView 0.3.0以后,拖动开始和停止完全取决于您。在DSLV中调用startDrag() 或者 stopDrag() 即可。需要注意的是,如果在调用startDrag() 函数的时候没有Touch事件,则拖动操作不会启动。在通常情况下您都无需关系开始拖动的操作,DragSortController 已经帮助你做好了。

DragSortController

DragSortController 类实现了一些常用的拖拽操作和删除List item的操作。该类实现了 View.OnTouchListener 接口来监听点击事件。同时也实现了FloatViewManager 接口。如果您在XML文件中没有使用默认的DragSortController,则您需要调用DSLV对象的 setFloatViewManager()  setOnTouchListener() 函数来设置一个DragSortController 。

DragSortController的默认行为认为List Item启用了拖动操作并且具有一个“拖动手柄”View,在DragSortController 中设置了“拖动手柄”View的资源id(如果 use_default_controller 是 true的话,拖动手柄的ID可以在XML文件中设置)。如果点击事件发生在一个List Item中的拖动手柄View上并且检测到了启动拖动操作的手势,则会启动一个拖动操作。



  • float_alpha: (float, 1.0) 拖动View的透明度。取值0到1 , 1代表不透明。Transparency of floating View. Value from 0 to 1 where 1 is opaque.

  • slide_shuffle_speed: (float, 0.7) 拖动View下方的View挤走的动画速度。Speed of shuffle animations underneath floating View. A value of 0 means a shuffle animation is always in progress, whereas a value of 1 means items snap from position to position without animation.
  • drop_animation_duration: (int, 150) 拖动放下时候的动画时间。Drop animation smoothly centers the floating View over the drop slot before destroying it. Duration in milliseconds.
  • remove_animation_duration: (int, 150) 删除一个ListView的空白区域消失的动画时间。Remove animation smoothly collapses the empty slot when an item is removed. Duration in milliseconds.
  • float_background_color: (color, BLACK) 拖动View的北背景色。默认情况下拖动View是当前拖动的Item的图像缓存。Set the background color of the floating View when using the default DragSortController. Floating View in this case is a snapshot of the list item to be dragged.
  • drag_start_mode: (enum, “onDown”) 设置启动拖动的手势。Sets the gesture for starting a drag.
    • “onDown”:当用户按下拖动手柄的时候启动拖动操作。 Drag starts when finger touches down on the drag handle.
    • “onDrag”: 当用户按下拖动手柄开始拖动的时候启动拖动操作。Drag starts when finger touches down on drag handle and then drags (allows item clicks and long clicks).
    • “onLongPress”:在拖动手柄上长按时候启动拖动操作。 Drag starts on drag handle long press (allows item clicks).
  • remove_enabled: (bool, false) 是否启用拖动删除的功能。Enable dragged item removal by one of the remove_mode options below.
  • remove_mode: (enum, “flingRight”) 设置启用删除的手势。Sets the gesture for removing the dragged item.
    • “clickRemove”:点击click_remove_id对应的View来删除。 Click on item child View with id click_remove_id.
    • “flingRight”: 快速向右滑动。Fling to the right; get outta here!
    • “flingLeft”: 快速向左滑动。Fling to the left; sayonara sucker!
    • “slideRight”:向左滑动的时候,Floating View会变得透明。透明后释放,删除Item。 Floating View fades as you slide your finger to the right; lifting while faded removes item.
    • “slideLeft”: 同上,向右滑动。Floating View fades as you slide your finger to the right; lifting while faded removes item.
  • click_remove_id: (id, 0) 当 remove_mode="clickRemove"并且remove_enabled="true"时候指定的删除一个Item的View。DragSortController使用了该属性。   Android resource id that points to a child View of a list item. When remove_mode="clickRemove" andremove_enabled="true", a click on this child View removes the containing item. This attr is used by DragSortController.




0 0
原创粉丝点击