android 随手记 可以拖动的Listview (二)

这篇文章是再cnblog 上看到的。具体的作者我已经记不得了。在这里感谢一下。废话不多说上代码。

1.写一个activity 

  1. package com.fengjian.test;  
  2.   
  3. import java.util.ArrayList;  
  4. import java.util.List;  
  5.   
  6. import android.app.Activity;  
  7. import android.content.Context;  
  8. import android.os.Bundle;  
  9. import android.view.LayoutInflater;  
  10. import android.view.View;  
  11. import android.view.ViewGroup;  
  12. import android.widget.ArrayAdapter;  
  13. import android.widget.TextView;  
  14.   
  15. public class DragListActivity extends Activity {  
  16.       
  17.     private static List<String> list = null;  
  18.     private DragListAdapter adapter = null;  
  19.       
  20.     public static List<String> groupKey= new ArrayList<String>();  
  21.     private List<String> navList = new ArrayList<String>();  
  22.     private List<String> moreList = new ArrayList<String>();  
  23.       
  24.     @Override  
  25.     public void onCreate(Bundle savedInstanceState) {  
  26.         super.onCreate(savedInstanceState);  
  27.         setContentView(R.layout.drag_list_activity);  
  28.           
  29.         initData();  
  30.           
  31.         DragListView dragListView = (DragListView)findViewById(R.id.drag_list);  
  32.         adapter = new DragListAdapter(this, list);  
  33.         dragListView.setAdapter(adapter);  
  34.     }  
  35.       
  36.     public void initData(){  
  37.         //数据结果  
  38.         list = new ArrayList<String>();  
  39.           
  40.         //groupKey存放的是分组标签  
  41.         groupKey.add("A组");  
  42.         groupKey.add("B组");  
  43.           
  44.         for(int i=0; i<5; i++){  
  45.             navList.add("A选项"+i);  
  46.         }  
  47.         list.add("A组");  
  48.         list.addAll(navList);  
  49.           
  50.         for(int i=0; i<8; i++){  
  51.             moreList.add("B选项"+i);  
  52.         }  
  53.         list.add("B组");  
  54.         list.addAll(moreList);  
  55.     }  
  56.       
  57.     public static class DragListAdapter extends ArrayAdapter<String>{  
  58.           
  59.         private Context mContext;  
  60.         public DragListAdapter(Context context, List<String> objects) {  
  61.             super(context, 0, objects);  
  62.             this.mContext=context;  
  63.         }  
  64.           
  65.         public List<String> getList(){  
  66.             return list;  
  67.         }  
  68.           
  69.         @Override  
  70.         public boolean isEnabled(int position) {  
  71.             if(groupKey.contains(getItem(position))){  
  72.                 //如果是分组标签,返回false,不能选中,不能点击  
  73.                 return false;  
  74.             }  
  75.             return super.isEnabled(position);  
  76.         }  
  77.   
  78.         @Override  
  79.         public View getView(int position, View convertView, ViewGroup parent) {  
  80.               
  81.             View view = convertView;  
  82.             if(groupKey.contains(getItem(position))){  
  83.                 //如果是分组标签,就加载分组标签的布局文件,两个布局文件显示效果不同  
  84.                 view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item_tag, null);  
  85.             }else{  
  86.                 //如果是正常数据项标签,就加在正常数据项的布局文件  
  87.                 view = LayoutInflater.from(getContext()).inflate(R.layout.drag_list_item, null);  
  88.             }  
  89.               
  90.             TextView textView = (TextView)view.findViewById(R.id.drag_list_item_text);  
  91.             textView.setText(getItem(position));  
  92.               
  93.             return view;  
  94.         }  
  95.     }  
  96. }  
2.定义一个自己的view 类
  1. package com.fengjian.test;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.Bitmap;  
  5. import android.graphics.PixelFormat;  
  6. import android.util.AttributeSet;  
  7. import android.view.Gravity;  
  8. import android.view.MotionEvent;  
  9. import android.view.View;  
  10. import android.view.ViewConfiguration;  
  11. import android.view.ViewGroup;  
  12. import android.view.WindowManager;  
  13. import android.widget.AdapterView;  
  14. import android.widget.ImageView;  
  15. import android.widget.ListView;  
  16. import android.widget.Toast;  
  17.   
  18. import com.fengjian.test.DragListActivity.DragListAdapter;  
  19.   
  20. public class DragListView extends ListView {  
  21.       
  22.     private ImageView dragImageView;//被拖拽的项,其实就是一个ImageView  
  23.     private int dragSrcPosition;//手指拖动项原始在列表中的位置  
  24.     private int dragPosition;//手指拖动的时候,当前拖动项在列表中的位置  
  25.       
  26.     private int dragPoint;//在当前数据项中的位置  
  27.     private int dragOffset;//当前视图和屏幕的距离(这里只使用了y方向上)  
  28.       
  29.     private WindowManager windowManager;//windows窗口控制类  
  30.     private WindowManager.LayoutParams windowParams;//用于控制拖拽项的显示的参数  
  31.       
  32.     private int scaledTouchSlop;//判断滑动的一个距离  
  33.     private int upScrollBounce;//拖动的时候,开始向上滚动的边界  
  34.     private int downScrollBounce;//拖动的时候,开始向下滚动的边界  
  35.       
  36.     public DragListView(Context context, AttributeSet attrs) {  
  37.         super(context, attrs);  
  38.         scaledTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();  
  39.     }  
  40.       
  41.     //拦截touch事件,其实就是加一层控制  
  42.     @Override  
  43.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  44.         if(ev.getAction()==MotionEvent.ACTION_DOWN){  
  45.             int x = (int)ev.getX();  
  46.             int y = (int)ev.getY();  
  47.               
  48.             dragSrcPosition = dragPosition = pointToPosition(x, y);  
  49.             if(dragPosition==AdapterView.INVALID_POSITION){  
  50.                 return super.onInterceptTouchEvent(ev);  
  51.             }  
  52.   
  53.             ViewGroup itemView = (ViewGroup) getChildAt(dragPosition-getFirstVisiblePosition());  
  54.             dragPoint = y - itemView.getTop();  
  55.             dragOffset = (int) (ev.getRawY() - y);  
  56.               
  57.             View dragger = itemView.findViewById(R.id.drag_list_item_image);  
  58.             if(dragger!=null&&x>dragger.getLeft()-20){  
  59.                 //  
  60.                 upScrollBounce = Math.min(y-scaledTouchSlop, getHeight()/3);  
  61.                 downScrollBounce = Math.max(y+scaledTouchSlop, getHeight()*2/3);  
  62.                   
  63.                 itemView.setDrawingCacheEnabled(true);  
  64.                 Bitmap bm = Bitmap.createBitmap(itemView.getDrawingCache());  
  65.                 startDrag(bm, y);  
  66.             }  
  67.             return false;  
  68.          }  
  69.          return super.onInterceptTouchEvent(ev);  
  70.     }  
  71.   
  72.     /** 
  73.      * 触摸事件 
  74.      */  
  75.     @Override  
  76.     public boolean onTouchEvent(MotionEvent ev) {  
  77.         if(dragImageView!=null&&dragPosition!=INVALID_POSITION){  
  78.             int action = ev.getAction();  
  79.             switch(action){  
  80.                 case MotionEvent.ACTION_UP:  
  81.                     int upY = (int)ev.getY();  
  82.                     stopDrag();  
  83.                     onDrop(upY);  
  84.                     break;  
  85.                 case MotionEvent.ACTION_MOVE:  
  86.                     int moveY = (int)ev.getY();  
  87.                     onDrag(moveY);  
  88.                     break;  
  89.                 default:break;  
  90.             }  
  91.             return true;  
  92.         }  
  93.         //也决定了选中的效果  
  94.         return super.onTouchEvent(ev);  
  95.     }  
  96.       
  97.     /** 
  98.      * 准备拖动,初始化拖动项的图像 
  99.      * @param bm 
  100.      * @param y 
  101.      */  
  102.     public void startDrag(Bitmap bm ,int y){  
  103.         stopDrag();  
  104.           
  105.         windowParams = new WindowManager.LayoutParams();  
  106.         windowParams.gravity = Gravity.TOP;  
  107.         windowParams.x = 0;  
  108.         windowParams.y = y - dragPoint + dragOffset;  
  109.         windowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;  
  110.         windowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  
  111.         windowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  
  112.                             | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE  
  113.                             | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON  
  114.                             | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;  
  115.         windowParams.format = PixelFormat.TRANSLUCENT;  
  116.         windowParams.windowAnimations = 0;  
  117.   
  118.         ImageView imageView = new ImageView(getContext());  
  119.         imageView.setImageBitmap(bm);  
  120.         windowManager = (WindowManager)getContext().getSystemService("window");  
  121.         windowManager.addView(imageView, windowParams);  
  122.         dragImageView = imageView;  
  123.     }  
  124.       
  125.     /** 
  126.      * 停止拖动,去除拖动项的头像 
  127.      */  
  128.     public void stopDrag(){  
  129.         if(dragImageView!=null){  
  130.             windowManager.removeView(dragImageView);  
  131.             dragImageView = null;  
  132.         }  
  133.     }  
  134.       
  135.     /** 
  136.      * 拖动执行,在Move方法中执行 
  137.      * @param y 
  138.      */  
  139.     public void onDrag(int y){  
  140.         if(dragImageView!=null){  
  141.             windowParams.alpha = 0.8f;  
  142.             windowParams.y = y - dragPoint + dragOffset;  
  143.             windowManager.updateViewLayout(dragImageView, windowParams);  
  144.         }  
  145.         //为了避免滑动到分割线的时候,返回-1的问题  
  146.         int tempPosition = pointToPosition(0, y);  
  147.         if(tempPosition!=INVALID_POSITION){  
  148.             dragPosition = tempPosition;  
  149.         }  
  150.           
  151.         //滚动  
  152.         int scrollHeight = 0;  
  153.         if(y<upScrollBounce){  
  154.             scrollHeight = 8;//定义向上滚动8个像素,如果可以向上滚动的话  
  155.         }else if(y>downScrollBounce){  
  156.             scrollHeight = -8;//定义向下滚动8个像素,,如果可以向上滚动的话  
  157.         }  
  158.           
  159.         if(scrollHeight!=0){  
  160.             //真正滚动的方法setSelectionFromTop()  
  161.             setSelectionFromTop(dragPosition, getChildAt(dragPosition-getFirstVisiblePosition()).getTop()+scrollHeight);  
  162.         }  
  163.     }  
  164.       
  165.     /** 
  166.      * 拖动放下的时候 
  167.      * @param y 
  168.      */  
  169.     public void onDrop(int y){  
  170.           
  171.         //为了避免滑动到分割线的时候,返回-1的问题  
  172.         int tempPosition = pointToPosition(0, y);  
  173.         if(tempPosition!=INVALID_POSITION){  
  174.             dragPosition = tempPosition;  
  175.         }  
  176.           
  177.         //超出边界处理  
  178.         if(y<getChildAt(1).getTop()){  
  179.             //超出上边界  
  180.             dragPosition = 1;  
  181.         }else if(y>getChildAt(getChildCount()-1).getBottom()){  
  182.             //超出下边界  
  183.             dragPosition = getAdapter().getCount()-1;  
  184.         }  
  185.           
  186.         //数据交换  
  187.         if(dragPosition>0&&dragPosition<getAdapter().getCount()){  
  188.             @SuppressWarnings("unchecked")  
  189.             DragListAdapter adapter = (DragListAdapter)getAdapter();  
  190.             String dragItem = adapter.getItem(dragSrcPosition);  
  191.             adapter.remove(dragItem);  
  192.             adapter.insert(dragItem, dragPosition);  
  193.             Toast.makeText(getContext(), adapter.getList().toString(), Toast.LENGTH_SHORT).show();  
  194.         }  
  195.           
  196.     }  
  197. }  

3.写layout 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <com.fengjian.test.DragListView   
  8.        android:id="@+id/drag_list"   
  9.        android:layout_width="fill_parent"   
  10.        android:layout_height="fill_parent"  
  11.        android:cacheColorHint="#00000000"/>  
  12. </LinearLayout>  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:background="#555555"  
  6.     android:padding="5dip"  
  7.     android:paddingLeft="10dip">  
  8.     <TextView  
  9.        android:id="@+id/drag_list_item_text"  
  10.        android:layout_width="wrap_content"  
  11.        android:layout_height="20dip"  
  12.        android:textColor="#ffffff"  
  13.        android:gravity="center_vertical"/>  
  14. </LinearLayout>  


  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 一定要使用相对布局 -->  
  3. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="wrap_content">  
  6.     <TextView  
  7.        android:id="@+id/drag_list_item_text"   
  8.        android:layout_width="wrap_content"   
  9.        android:layout_height="@dimen/drag_item_normal_height"  
  10.        android:paddingLeft="5dip"  
  11.        android:layout_alignParentLeft="true"  
  12.        android:layout_centerVertical="true"  
  13.        android:gravity="center_vertical" android:capitalize="none"/>  
  14.     <ImageView android:id="@+id/drag_list_item_image"  
  15.        android:src="@drawable/list_icon"  
  16.        android:layout_alignParentRight="true"  
  17.        android:layout_centerVertical="true"  
  18.        android:layout_width="wrap_content"  
  19.        android:layout_height="@dimen/drag_item_normal_height"/>  
  20. </RelativeLayout>  
其实主要的就是上面自己定义的view 的类。照片我就不上传了。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值