Andriod 实现可拖动列表


在做一个Android应用时,有一个需求,需要实现像iphone天气的城市列表界面的可拖动功能。其实android已经实现了这个控件。但是这个控件不是公共控件,而是自带音乐播放器下的一个自定义控件。具体目录在:packages/apps/Music/src/com/android/music/TouchInterceptor.java。

使用发方法很简单,因为TouchInterceptor.java是继承ListView,与Listview不同之处在于,需要注册对该Listview的监听

代码如下:

  1. public void setTrashcan(Drawable trash) {  
  2.     mTrashcan = trash;  
  3.     mRemoveMode = TRASH;  
  4. }  
  5.   
  6. public void setDragListener(DragListener l) {  
  7.     mDragListener = l;  
  8. }  
  9.   
  10. public void setDropListener(DropListener l) {  
  11.     mDropListener = l;  
  12. }  
  13.   
  14. public void setRemoveListener(RemoveListener l) {  
  15.     mRemoveListener = l;  
  16. }  
  17.   
  18. public interface DragListener {  
  19.     void drag(int from, int to);  
  20. }  
  21. public interface DropListener {//  拖动listview的item,如将position=1的拖动到position=5,在这里做必要数据更新   
  22.     void drop(int from, int to);  
  23. }  
  24. public interface RemoveListener {  
  25.     void remove(int which);  
  26. }  

相关内容