`

draggable可拖动的ListView,并且支持行删除功能

阅读更多
看图,拖动前:

拖动后:


package com.ql.view;

import com.ql.activity.R;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;

/**
 * draggable可拖动的列表,并且支持行删除功能
 * @author admin
 *
 */
public class DraggableListView extends ListView {

	private DropListener mDropListener;

	private ImageView mDragView;
	private int mDragPos; // which item is being dragged
	private int mFirstDragPos; // where was the dragged item originally
	private int mDragPoint; // at what offset inside the item did the user grab
							// it
	private int mCoordOffset; // the difference between screen coordinates and
								// coordinates in this view

	private Rect mTempRect = new Rect();
	private final int mTouchSlop;
	private int mHeight;
	private int mUpperBound;
	private int mLowerBound;
	private WindowManager mWindowManager;
	private WindowManager.LayoutParams mWindowParams;
	private int dragndropBackgroundColor = 0x00000000;
	private Bitmap mDragBitmap;
	private int mItemHeightHalf = 32;
	private int mItemHeightNormal = 64;
	private int mItemHeightExpanded = 128;
	//private int grabberId=-1;

	public DraggableListView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
		// TODO Auto-generated constructor stub
	}

	public DraggableListView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		// TODO Auto-generated constructor stub
		mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
		
//		if (attrs!=null) {
//			TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.TouchListView,0, 0);
//
//			mItemHeightNormal=a.getDimensionPixelSize(R.styleable.TouchListView_normal_height, 0);
//			mItemHeightExpanded=a.getDimensionPixelSize(R.styleable.TouchListView_expanded_height, mItemHeightNormal);
//			grabberId=a.getResourceId(R.styleable.TouchListView_grabber, -1);
//			dragndropBackgroundColor=a.getColor(R.styleable.TouchListView_dragndrop_background, 0x00000000);
//			//mRemoveMode=a.getInt(R.styleable.TouchListView_remove_mode, -1);
//
//			a.recycle();
//		}

	}

	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
//		Log.v(">>>>>>>>>>onTouchEvent", ">>>>>>>>>>onTouchEvent");
		if ((mDropListener != null) && mDragView != null) {
			int action = ev.getAction();
			switch (action) {
			case MotionEvent.ACTION_UP:
			case MotionEvent.ACTION_CANCEL:
				Rect r = mTempRect;
				mDragView.getDrawingRect(r);
				stopDragging();
				if (mDropListener != null && mDragPos >= 0
						&& mDragPos < getCount()) {
					mDropListener.drop(mFirstDragPos, mDragPos);
				}
				unExpandViews(false);
				break;

			case MotionEvent.ACTION_DOWN:
			case MotionEvent.ACTION_MOVE:
				int x = (int) ev.getX();
				int y = (int) ev.getY();
				dragView(x, y);

				int itemnum = getItemForPosition(y);
				if (itemnum >= 0) {
					if (action == MotionEvent.ACTION_DOWN
							|| itemnum != mDragPos) {

						mDragPos = itemnum;
						doExpansion();
//						Log.v(">>>doExpansion", ">>>>>>>>>>doExpansion");
					}
					/*
					int speed = 0;
					adjustScrollBounds(y);
					if (y > mLowerBound) {
						// scroll the list up a bit
						speed = y > (mHeight + mLowerBound) / 2 ? 16 : 4;
					} else if (y < mUpperBound) {
						// scroll the list down a bit
						speed = y < mUpperBound / 2 ? -16 : -4;
					}
					if (speed != 0) {
						int ref = pointToPosition(0, mHeight / 2);
						if (ref == AdapterView.INVALID_POSITION) {
							// we hit a divider or an invisible view, check
							// somewhere else
							ref = pointToPosition(0, mHeight / 2
									+ getDividerHeight() + 64);
						}
						View v = getChildAt(ref - getFirstVisiblePosition());
						if (v != null) {
							int pos = v.getTop();
							setSelectionFromTop(ref, pos - speed);

						}
					}
					*/
				}
				break;
			}
			return true;
		}
		return super.onTouchEvent(ev);

	}

	@Override
	public boolean onInterceptTouchEvent(MotionEvent ev) {
		// TODO Auto-generated method stub
		if (mDropListener != null) {
			switch (ev.getAction()) {
			case MotionEvent.ACTION_DOWN:
				int x = (int) ev.getX();
				int y = (int) ev.getY();
				int itemnum = pointToPosition(x, y);
//				Log.v("itemnum>>>", ">>>>>>>>" + itemnum);
				if (itemnum == AdapterView.INVALID_POSITION) {
					break;
				}
				ViewGroup item = (ViewGroup) getChildAt(itemnum
						- getFirstVisiblePosition());
//				Log.v("itemnum>>>", ">>>>>>>>" + getFirstVisiblePosition()
//						+ "---" + ev.getRawY() + "----" + ev.getY()+"-----"+item.getTop());
				mDragPoint = y - item.getTop();
				mCoordOffset = ((int) ev.getRawY()) - y;
				View dragger = item.findViewById(R.id.iconimg);//拖动的ImageView
				Rect r = mTempRect;
				// dragger.getDrawingRect(r);

				r.left = dragger.getLeft();
				r.right = dragger.getRight();
				r.top = dragger.getTop();
				r.bottom = dragger.getBottom();

				if ((r.left < x) && (x < r.right)) {
					item.setDrawingCacheEnabled(true);
					// Create a copy of the drawing cache so that it does not
					// get recycled
					// by the framework when the list tries to clean up memory
					Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());
					startDragging(bitmap, y);
					mDragPos = itemnum;
					mFirstDragPos = mDragPos;
					mHeight = getHeight();
					int touchSlop = mTouchSlop;
					mUpperBound = Math.min(y - touchSlop, mHeight / 3);
					mLowerBound = Math.max(y + touchSlop, mHeight * 2 / 3);
					return false;
				}
				//
				View delView = item.findViewById(R.id.delete);//删除的ImageView
				r.left = delView.getLeft();
				r.right = delView.getRight();
				r.top = delView.getTop();
				r.bottom = delView.getBottom();
				if ((r.left < x) && (x < r.right)) {
					mDropListener.onDeleteClicked(itemnum);
					return false;
				}

				mDragView = null;
				break;
			}
		}
		return super.onInterceptTouchEvent(ev);
	}

	private void startDragging(Bitmap bm, int y) {
		stopDragging();

		mWindowParams = new WindowManager.LayoutParams();
		mWindowParams.gravity = Gravity.TOP;
		mWindowParams.x = 0;
		mWindowParams.y = y - mDragPoint + mCoordOffset;

		mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;
		mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;
		mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
				| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
				| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON
				| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN;
		mWindowParams.format = PixelFormat.TRANSLUCENT;
		mWindowParams.windowAnimations = 0;

		ImageView v = new ImageView(getContext());
		// int backGroundColor =
		// getContext().getResources().getColor(R.color.dragndrop_background);
		v.setBackgroundColor(dragndropBackgroundColor);
		v.setImageBitmap(bm);
		mDragBitmap = bm;

		mWindowManager = (WindowManager) getContext()
				.getSystemService("window");
		mWindowManager.addView(v, mWindowParams);
		mDragView = v;
	}

	private void stopDragging() {
		if (mDragView != null) {
			WindowManager wm = (WindowManager) getContext().getSystemService(
					"window");
			wm.removeView(mDragView);
			mDragView.setImageDrawable(null);
			mDragView = null;
		}
		if (mDragBitmap != null) {
			mDragBitmap.recycle();
			mDragBitmap = null;
		}
	}

	private void dragView(int x, int y) {
		float alpha = 1.0f;
		mWindowParams.alpha = alpha;
		// }
		mWindowParams.y = y - mDragPoint + mCoordOffset;
		mWindowManager.updateViewLayout(mDragView, mWindowParams);
	}

	private int getItemForPosition(int y) {
		int adjustedy = y - mDragPoint - mItemHeightHalf;
		int pos = myPointToPosition(0, adjustedy);
		if (pos >= 0) {
			if (pos <= mFirstDragPos) {
				pos += 1;
			}
		} else if (adjustedy < 0) {
			pos = 0;
		}
		return pos;
	}

	private void adjustScrollBounds(int y) {
		if (y >= mHeight / 3) {
			mUpperBound = mHeight / 3;
		}
		if (y <= mHeight * 2 / 3) {
			mLowerBound = mHeight * 2 / 3;
		}
	}

	/*
	 * Restore size and visibility for all listitems
	 */
	private void unExpandViews(boolean deletion) {
		for (int i = 0;; i++) {
			View v = getChildAt(i);
			if (v == null) {
				if (deletion) {
					// HACK force update of mItemCount
					int position = getFirstVisiblePosition();
					int y = getChildAt(0).getTop();
					setAdapter(getAdapter());
					setSelectionFromTop(position, y);
					// end hack
				}
				layoutChildren(); // force children to be recreated where needed
				v = getChildAt(i);
				if (v == null) {
					break;
				}
			}
			ViewGroup.LayoutParams params = v.getLayoutParams();
			params.height = mItemHeightNormal;
			v.setLayoutParams(params);
			v.setVisibility(View.VISIBLE);
		}
	}

	/*
	 * Adjust visibility and size to make it appear as though an item is being
	 * dragged around and other items are making room for it: If dropping the
	 * item would result in it still being in the same place, then make the
	 * dragged listitem's size normal, but make the item invisible. Otherwise,
	 * if the dragged listitem is still on screen, make it as small as possible
	 * and expand the item below the insert point. If the dragged item is not on
	 * screen, only expand the item below the current insertpoint.
	 */
	private void doExpansion() {
		int childnum = mDragPos - getFirstVisiblePosition();
		if (mDragPos > mFirstDragPos) {
			childnum++;
		}

		View first = getChildAt(mFirstDragPos - getFirstVisiblePosition());

		for (int i = 0;; i++) {
			View vv = getChildAt(i);
			if (vv == null) {
				break;
			}
			int height = mItemHeightNormal;
			int visibility = View.VISIBLE;
			if (vv.equals(first)) {
				// processing the item that is being dragged
				if (mDragPos == mFirstDragPos) {
					// hovering over the original location
					visibility = View.INVISIBLE;
				} else {
					// not hovering over it
					height = 1;
				}
			} else if (i == childnum) {
				if (mDragPos < getCount() - 1) {
					height = mItemHeightExpanded;
				}
			}
			ViewGroup.LayoutParams params = vv.getLayoutParams();
			params.height = height;
			vv.setLayoutParams(params);
			vv.setVisibility(visibility);
		}
	}

	/*
	 * pointToPosition() doesn't consider invisible views, but we need to, so
	 * implement a slightly different version.
	 */
	private int myPointToPosition(int x, int y) {
		Rect frame = mTempRect;
		final int count = getChildCount();
		for (int i = count - 1; i >= 0; i--) {
			final View child = getChildAt(i);
			child.getHitRect(frame);
			if (frame.contains(x, y)) {
				return getFirstVisiblePosition() + i;
			}
		}
		return INVALID_POSITION;
	}

	public interface DropListener {
		//
		void drop(int from, int to);
		//
		void onDeleteClicked(int index);
	}

	public void setDropListener(DropListener onDrop) {
		// TODO Auto-generated method stub
		mDropListener = onDrop;
	}

}


使用:
package com.ql.activity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;


import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.ql.view.DraggableListView;


public class Test_6_Activity  extends Activity{
	private DraggableListView	mListView;
	private DraggableArrayAdapter adapter = null;
	private ArrayList<Map<String, String>> array;
//	private	int	mIndex;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.test5);
		
		mListView = (DraggableListView) findViewById(R.id.draggable_list);  
		array = getData();
		adapter = new DraggableArrayAdapter();
		mListView.setAdapter(adapter);
		
		mListView.setDropListener(onDrop);
//		mListView.getAdapter();
//		//列表点击事件处理
//        mListView.setOnItemClickListener(new OnItemClickListener() {
//
//        	@Override
//        	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
//        							long arg3) {
//        		
//        		//Toast.makeText(context, "Click "+arg2, Toast.LENGTH_SHORT).show();
//        	}
//        });   
//        
//        mListView.setOnItemLongClickListener(new OnItemLongClickListener() {
//        	
//        	@Override
//        	public boolean onItemLongClick(AdapterView<?> arg0, View arg1, int position,
//        			long id) {
//        		mIndex = position;
//        		return false;
//        	}
//        });
	
	}
	//初始化数据
	private ArrayList<Map<String, String>> getData() {
		ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();
		for(int i=0; i<5; i++) {
			Map<String, String> map = new HashMap<String, String>();
			map.put("code", "code"+i);
			map.put("name", "name"+i);
			list.add(map);
		}
		return list;
	}

	private DraggableListView.DropListener onDrop = new DraggableListView.DropListener() {
		@Override
		public void drop(int from, int to) {
			Map<String, String> item = adapter.getItem(from);
			adapter.remove(item);
			adapter.insert(item, to);
		}

		@Override
		public void onDeleteClicked(int index) {
//			mIndex = index;
			delete(index);
		}
	};
	private void delete(final int index) {
		String prompt = "delete "+array.get(index).get("name").toString()+" "+array.get(index).get("code").toString()+"?";
		//删除确认对话		
		new AlertDialog.Builder(Test_6_Activity.this)
			.setTitle("删除?")
			//.setIcon(android.R.drawable.ic_menu_help)
			.setMessage(prompt)
			.setCancelable(true)
			.setPositiveButton("确定", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					Map<String, String> item = adapter.getItem(index);
					adapter.remove(item);
				}
			})
			.setNegativeButton("取消", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					dialog.cancel();
				}
			}).show();

	}
	/**
	 * 适配器
	 * @author admin
	 *
	 */
	class DraggableArrayAdapter extends ArrayAdapter<Map<String, String>> {

		DraggableArrayAdapter() {
			
			super(Test_6_Activity.this, R.layout.row_simple_list_item_4, array);
		}

		public ArrayList<Map<String, String>> getList() {
			return array;
		}

		public View getView(int position, View convertView, ViewGroup parent) {
			
			View row = convertView;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater.inflate(R.layout.row_simple_list_item_4, parent, false);
			}
			TextView code = (TextView) row.findViewById(R.id.code);
			code.setText(array.get(position).get("code").toString());
			TextView name = (TextView) row.findViewById(R.id.name);
			name.setText(array.get(position).get("name").toString());

			return (row);
		}
	}
}


text5.xml布局文件:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
  	android:orientation="vertical"
  	android:layout_width="fill_parent"
  	android:layout_height="fill_parent">

	<com.ql.view.DraggableListView
		android:id="@+id/draggable_list"
		android:layout_width="fill_parent" 
		android:layout_height="fill_parent"
		/>
</LinearLayout>

适配器中使用的布局文件row_simple_list_item_4.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    >
    <!-- 
    android:background="@drawable/list_bg"
     -->
    <ImageView android:id="@+id/iconimg"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_alignParentBottom="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
		android:src="@drawable/grabber"
		/>
		
    <ImageView android:id="@+id/delete"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
    	android:paddingRight="5dip"
        android:src="@android:drawable/ic_delete"
		/>
		
	<TextView android:id="@+id/code"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip"
        android:layout_toRightOf="@id/iconimg"
		android:layout_centerVertical="true"
		android:textSize="20dip"
		android:textColor="#888888"
		/>
	<TextView android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="10dip"
        android:layout_toRightOf="@id/code"
		android:layout_centerVertical="true"
		android:textSize="20dip"
		android:textColor="#888888"
		/>
		
</RelativeLayout>



另一个ListView:
http://blog.csdn.net/sodino/archive/2010/12/15/6077017.aspx
1.实现根据字母进行分类。
2.实现快速滑动。
3.实现快速滑动的提示。
4.实现快捷弹窗。

自定义Android ListView控件:ExpandableListView
http://www.pin5i.com/showtopic-custom-android-listview-control-expandablelistview.html

另一篇关于listview的拖动效果
android listview拖拽,拖动item 改变位置
http://blog.csdn.net/dany1202/archive/2010/12/31/6109160.aspx
在packages/apps/Music/src/touchIncepter.java中
该类提供了listview的拖动效果,并提供接口,在程序接口中实现数据的交换即可。
package com.and.DragListview;  
import java.util.ArrayList;  
import java.util.List;  
import android.app.ListActivity;  
import android.content.Context;  
import android.os.Bundle;  
import android.view.LayoutInflater;  
import android.view.View;  
import android.view.ViewGroup;  
import android.widget.BaseAdapter;  
import android.widget.ImageView;  
import android.widget.TextView;  
public class DragListview extends ListActivity {     
    MyAdapter adapter;  
    TouchInterceptor list;  
    List<String> arrayText;  
      
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
          
        list = (TouchInterceptor) getListView();//(TouchInterceptor)findViewById(android.R.id.list);  
        getText();  
          
        adapter = new MyAdapter(this);  
        setListAdapter(adapter);  
         
        list.setDropListener(mDropListener);  
     //   list.setRemoveListener(mRemoveListener);        
    }  
    public void getText(){  
        arrayText = new ArrayList<String>();  
        arrayText.add("传奇");  
        arrayText.add("红豆");  
        arrayText.add("流年");  
        arrayText.add("棋子");  
    }  
      
    //交换listview的数据  
    private TouchInterceptor.DropListener mDropListener =  
        new TouchInterceptor.DropListener() {  
        public void drop(int from, int to) {  
            String item = arrayText.get(from);  
            arrayText.remove(item);//.remove(from);  
            arrayText.add(to, item);  
            adapter.notifyDataSetChanged();  
        }  
    };  
      
    private TouchInterceptor.RemoveListener mRemoveListener =  
        new TouchInterceptor.RemoveListener() {  
        public void remove(int which) {            
        }  
    };  
      
    class MyAdapter extends BaseAdapter{  
        private LayoutInflater mInflater;  
        Context mContext;  
        public MyAdapter(Context c){  
            mInflater = LayoutInflater.from(c);  
        }  
        public int getCount() {           
            return arrayText.size();  
        }  
        public Object getItem(int arg0) {  
            return arrayText.get(arg0);  
        }  
        public long getItemId(int arg0) {  
            return arg0;  
        }  
        public View getView(int arg0, View contentView, ViewGroup arg2) {  
            ImageView img;  
            TextView text;  
            if(contentView==null){  
                contentView = mInflater.inflate(R.layout.list_layout, null);   
                //contentView = mInflater.inflate(R.layout.list_layout,null);  
            }  
            img = (ImageView)contentView.findViewById(R.id.img);  
            img.setBackgroundResource(R.drawable.icon);  
            text = (TextView)contentView.findViewById(R.id.text);  
            text.setText(arrayText.get(arg0).toString());  
              
            return contentView;  
        }  
          
    }  
}  

/* 
 * Copyright (C) 2008 The Android Open Source Project 
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */  
package com.and.DragListview;  
import android.content.Context;  
import android.content.SharedPreferences;  
import android.content.res.Resources;  
import android.graphics.Bitmap;  
import android.graphics.PixelFormat;  
import android.graphics.Rect;  
import android.util.AttributeSet;  
import android.view.GestureDetector;  
import android.view.Gravity;  
import android.view.MotionEvent;  
import android.view.View;  
import android.view.ViewConfiguration;  
import android.view.ViewGroup;  
import android.view.WindowManager;  
import android.view.GestureDetector.SimpleOnGestureListener;  
import android.widget.AdapterView;  
import android.widget.ImageView;  
import android.widget.ListView;  
public class TouchInterceptor extends ListView {  
      
    private ImageView mDragView;  
    private WindowManager mWindowManager;  
    private WindowManager.LayoutParams mWindowParams;  
    private int mDragPos;      // which item is being dragged  
    private int mFirstDragPos; // where was the dragged item originally  
    private int mDragPoint;    // at what offset inside the item did the user grab it  
    private int mCoordOffset;  // the difference between screen coordinates and coordinates in this view  
    private DragListener mDragListener;  
    private DropListener mDropListener;  
    private RemoveListener mRemoveListener;  
    private int mUpperBound;  
    private int mLowerBound;  
    private int mHeight;  
    private GestureDetector mGestureDetector;  
    private static final int FLING = 0;  
    private static final int SLIDE = 1;  
    private int mRemoveMode = -1;  
    private Rect mTempRect = new Rect();  
    private Bitmap mDragBitmap;  
    private final int mTouchSlop;  
    private int mItemHeightNormal;  
    private int mItemHeightExpanded;  
    private int mItemHeightHalf;  
    public TouchInterceptor(Context context, AttributeSet attrs) {  
        super(context, attrs);  
        SharedPreferences pref = context.getSharedPreferences("Music", 3);  
        mRemoveMode = pref.getInt("deletemode", -1);  
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();  
        Resources res = getResources();  
        mItemHeightNormal = 48;//res.getDimensionPixelSize(R.dimen.normal_height);  
        mItemHeightHalf = mItemHeightNormal / 2;  
        mItemHeightExpanded = 48;//res.getDimensionPixelSize(R.dimen.expanded_height);  
    }  
      
    @Override  
    public boolean onInterceptTouchEvent(MotionEvent ev) {  
        if (mRemoveListener != null && mGestureDetector == null) {  
            if (mRemoveMode == FLING) {  
                mGestureDetector = new GestureDetector(getContext(), new SimpleOnGestureListener() {  
                    @Override  
                    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
                            float velocityY) {  
                        if (mDragView != null) {  
                            if (velocityX > 1000) {  
                                Rect r = mTempRect;  
                                mDragView.getDrawingRect(r);  
                                if ( e2.getX() > r.right * 2 / 3) {  
                                    // fast fling right with release near the right edge of the screen  
                                    stopDragging();  
                                    mRemoveListener.remove(mFirstDragPos);  
                                    unExpandViews(true);  
                                }  
                            }  
                            // flinging while dragging should have no effect  
                            return true;  
                        }  
                        return false;  
                    }  
                });  
            }  
        }  
        if (mDragListener != null || mDropListener != null) {  
            switch (ev.getAction()) {  
                case MotionEvent.ACTION_DOWN:  
                    int x = (int) ev.getX();  
                    int y = (int) ev.getY();  
                    int itemnum = pointToPosition(x, y);  
                    if (itemnum == AdapterView.INVALID_POSITION) {  
                        break;  
                    }  
                    ViewGroup item = (ViewGroup) getChildAt(itemnum - getFirstVisiblePosition());  
                    mDragPoint = y - item.getTop();  
                    mCoordOffset = ((int)ev.getRawY()) - y;  
                    View dragger = item.findViewById(R.id.img);//..........................  
                    Rect r = mTempRect;  
                    dragger.getDrawingRect(r);  
                    // The dragger icon itself is quite small, so pretend the touch area is bigger  
                    if (x < r.right * 2) {  
                        item.setDrawingCacheEnabled(true);  
                        // Create a copy of the drawing cache so that it does not get recycled  
                        // by the framework when the list tries to clean up memory  
                        Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());  
                        startDragging(bitmap, y);  
                        mDragPos = itemnum;  
                        mFirstDragPos = mDragPos;  
                        mHeight = getHeight();  
                        int touchSlop = mTouchSlop;  
                        mUpperBound = Math.min(y - touchSlop, mHeight / 3);  
                        mLowerBound = Math.max(y + touchSlop, mHeight * 2 /3);  
                        return false;  
                    }  
                    stopDragging();  
                    break;  
            }  
        }  
        return super.onInterceptTouchEvent(ev);  
    }  
      
    /* 
     * pointToPosition() doesn't consider invisible views, but we 
     * need to, so implement a slightly different version. 
     */  
    private int myPointToPosition(int x, int y) {  
        if (y < 0) {  
            // when dragging off the top of the screen, calculate position  
            // by going back from a visible item  
            int pos = myPointToPosition(x, y + mItemHeightNormal);  
            if (pos > 0) {  
                return pos - 1;  
            }  
        }  
        Rect frame = mTempRect;  
        final int count = getChildCount();  
        for (int i = count - 1; i >= 0; i--) {  
            final View child = getChildAt(i);  
            child.getHitRect(frame);  
            if (frame.contains(x, y)) {  
                return getFirstVisiblePosition() + i;  
            }  
        }  
        return INVALID_POSITION;  
    }  
      
    private int getItemForPosition(int y) {  
        int adjustedy = y - mDragPoint - mItemHeightHalf;  
        int pos = myPointToPosition(0, adjustedy);  
        if (pos >= 0) {  
            if (pos <= mFirstDragPos) {  
                pos += 1;  
            }  
        } else if (adjustedy < 0) {  
            // this shouldn't happen anymore now that myPointToPosition deals  
            // with this situation  
            pos = 0;  
        }  
        return pos;  
    }  
      
    private void adjustScrollBounds(int y) {  
        if (y >= mHeight / 3) {  
            mUpperBound = mHeight / 3;  
        }  
        if (y <= mHeight * 2 / 3) {  
            mLowerBound = mHeight * 2 / 3;  
        }  
    }  
    /* 
     * Restore size and visibility for all listitems 
     */  
    private void unExpandViews(boolean deletion) {  
        for (int i = 0;; i++) {  
            View v = getChildAt(i);  
            if (v == null) {  
                if (deletion) {  
                    // HACK force update of mItemCount  
                    int position = getFirstVisiblePosition();  
                    int y = getChildAt(0).getTop();  
                    setAdapter(getAdapter());  
                    setSelectionFromTop(position, y);  
                    // end hack  
                }  
                layoutChildren(); // force children to be recreated where needed  
                v = getChildAt(i);  
                if (v == null) {  
                    break;  
                }  
            }  
            ViewGroup.LayoutParams params = v.getLayoutParams();  
            params.height = mItemHeightNormal;  
            v.setLayoutParams(params);  
            v.setVisibility(View.VISIBLE);  
        }  
    }  
      
    /* Adjust visibility and size to make it appear as though 
     * an item is being dragged around and other items are making 
     * room for it: 
     * If dropping the item would result in it still being in the 
     * same place, then make the dragged listitem's size normal, 
     * but make the item invisible. 
     * Otherwise, if the dragged listitem is still on screen, make 
     * it as small as possible and expand the item below the insert 
     * point. 
     * If the dragged item is not on screen, only expand the item 
     * below the current insertpoint. 
     */  
    private void doExpansion() {  
        int childnum = mDragPos - getFirstVisiblePosition();  
        if (mDragPos > mFirstDragPos) {  
            childnum++;  
        }  
        View first = getChildAt(mFirstDragPos - getFirstVisiblePosition());  
        for (int i = 0;; i++) {  
            View vv = getChildAt(i);  
            if (vv == null) {  
                break;  
            }  
            int height = mItemHeightNormal;  
            int visibility = View.VISIBLE;  
            if (vv.equals(first)) {  
                // processing the item that is being dragged  
                if (mDragPos == mFirstDragPos) {  
                    // hovering over the original location  
                    visibility = View.INVISIBLE;  
                } else {  
                    // not hovering over it  
                    height = 1;  
                }  
            } else if (i == childnum) {  
                if (mDragPos < getCount() - 1) {  
                    height = mItemHeightExpanded;  
                }  
            }  
            ViewGroup.LayoutParams params = vv.getLayoutParams();  
            params.height = height;  
            vv.setLayoutParams(params);  
            vv.setVisibility(visibility);  
        }  
    }  
      
    @Override  
    public boolean onTouchEvent(MotionEvent ev) {  
        if (mGestureDetector != null) {  
            mGestureDetector.onTouchEvent(ev);  
        }  
        if ((mDragListener != null || mDropListener != null) && mDragView != null) {  
            int action = ev.getAction();   
            switch (action) {  
                case MotionEvent.ACTION_UP:  
                case MotionEvent.ACTION_CANCEL:  
                    Rect r = mTempRect;  
                    mDragView.getDrawingRect(r);  
                    stopDragging();  
                    if (mRemoveMode == SLIDE && ev.getX() > r.right * 3 / 4) {  
                        if (mRemoveListener != null) {  
                            mRemoveListener.remove(mFirstDragPos);  
                        }  
                        unExpandViews(true);  
                    } else {  
                        if (mDropListener != null && mDragPos >= 0 && mDragPos < getCount()) {  
                            mDropListener.drop(mFirstDragPos, mDragPos);  
                        }  
                        unExpandViews(false);  
                    }  
                    break;  
                      
                case MotionEvent.ACTION_DOWN:  
                case MotionEvent.ACTION_MOVE:  
                    int x = (int) ev.getX();  
                    int y = (int) ev.getY();  
                    dragView(x, y);  
                    int itemnum = getItemForPosition(y);  
                    if (itemnum >= 0) {  
                        if (action == MotionEvent.ACTION_DOWN || itemnum != mDragPos) {  
                            if (mDragListener != null) {  
                                mDragListener.drag(mDragPos, itemnum);  
                            }  
                            mDragPos = itemnum;  
                            doExpansion();  
                        }  
                        int speed = 0;  
                        adjustScrollBounds(y);  
                        if (y > mLowerBound) {  
                            // scroll the list up a bit  
                            speed = y > (mHeight + mLowerBound) / 2 ? 16 : 4;  
                        } else if (y < mUpperBound) {  
                            // scroll the list down a bit  
                            speed = y < mUpperBound / 2 ? -16 : -4;  
                        }  
                        if (speed != 0) {  
                            int ref = pointToPosition(0, mHeight / 2);  
                            if (ref == AdapterView.INVALID_POSITION) {  
                                //we hit a divider or an invisible view, check somewhere else  
                                ref = pointToPosition(0, mHeight / 2 + getDividerHeight() + 64);  
                            }  
                            View v = getChildAt(ref - getFirstVisiblePosition());  
                            if (v!= null) {  
                                int pos = v.getTop();  
                                setSelectionFromTop(ref, pos - speed);  
                            }  
                        }  
                    }  
                    break;  
            }  
            return true;  
        }  
        return super.onTouchEvent(ev);  
    }  
      
    private void startDragging(Bitmap bm, int y) {  
        stopDragging();  
        mWindowParams = new WindowManager.LayoutParams();  
        mWindowParams.gravity = Gravity.TOP;  
        mWindowParams.x = 0;  
        mWindowParams.y = y - mDragPoint + mCoordOffset;  
        mWindowParams.height = WindowManager.LayoutParams.WRAP_CONTENT;  
        mWindowParams.width = WindowManager.LayoutParams.WRAP_CONTENT;  
        mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  
                | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE  
                | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON  
                | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN  
                | WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;  
        mWindowParams.format = PixelFormat.TRANSLUCENT;  
        mWindowParams.windowAnimations = 0;  
          
        Context context = getContext();  
        ImageView v = new ImageView(context);  
//        int backGroundColor = context.getResources().getColor(R.color.dragndrop_background);  
//        v.setBackgroundColor(backGroundColor);  
        v.setImageBitmap(bm);  
        mDragBitmap = bm;  
        mWindowManager = (WindowManager)context.getSystemService("window");  
        mWindowManager.addView(v, mWindowParams);  
        mDragView = v;  
    }  
      
    private void dragView(int x, int y) {  
        if (mRemoveMode == SLIDE) {  
            float alpha = 1.0f;  
            int width = mDragView.getWidth();  
            if (x > width / 2) {  
                alpha = ((float)(width - x)) / (width / 2);  
            }  
            mWindowParams.alpha = alpha;  
        }  
        if (mRemoveMode == FLING) {  
            mWindowParams.x = x;  
        }  
        mWindowParams.y = y - mDragPoint + mCoordOffset;  
        mWindowManager.updateViewLayout(mDragView, mWindowParams);  
    }  
      
    private void stopDragging() {  
        if (mDragView != null) {  
            WindowManager wm = (WindowManager)getContext().getSystemService("window");  
            wm.removeView(mDragView);  
            mDragView.setImageDrawable(null);  
            mDragView = null;  
        }  
        if (mDragBitmap != null) {  
            mDragBitmap.recycle();  
            mDragBitmap = null;  
        }  
    }  
      
    public void setDragListener(DragListener l) {  
        mDragListener = l;  
    }  
      
    public void setDropListener(DropListener l) {  
        mDropListener = l;  
    }  
      
    public void setRemoveListener(RemoveListener l) {  
        mRemoveListener = l;  
    }  
    public interface DragListener {  
        void drag(int from, int to);  
    }  
    public interface DropListener {  
        void drop(int from, int to);  
    }  
    public interface RemoveListener {  
        void remove(int which);  
    }  
}  

<com.and.DragListview.TouchInterceptor
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"       
        android:textSize="18sp"
        android:drawSelectorOnTop="false"
        android:fastScrollEnabled="true" />


还有这个:
http://www.eoeandroid.com/thread-61490-1-1.html

Android学习系列(12)--App列表之拖拽GridView
http://www.cnblogs.com/qianxudetianxia/archive/2011/06/20/2084886.html
  • 大小: 17.2 KB
  • 大小: 17 KB
分享到:
评论
发表评论

文章已被作者锁定,不允许评论。

相关推荐

    可拖动listview

    总的来说,实现可拖动的ListView并支持分组管理涉及到了Android的Drag and Drop API、自定义View、数据模型和适配器的改造等多个方面。通过这些技术,我们可以创建出更加互动和用户友好的应用界面,提升用户体验。在...

    可以拖拽的ListView

    "可以拖拽的ListView"正是针对这一需求的一种扩展功能,它允许用户通过手势直接拖动ListView中的条目(Item)并重新排列它们的位置,提升了用户体验。 实现拖拽功能的关键在于对触摸事件的处理和ListView的适配器...

    ListView拖拽移位具体实现

    拖拽移位功能在许多应用中非常实用,比如文件管理器、任务列表等,用户可以通过拖动ListView中的条目来改变它们的顺序。本篇文章将详细介绍如何在Android中实现ListView的拖拽移位功能。 首先,我们需要创建一个...

    andoid拖拽ListView

    在Android开发中,ListView是一种常用的组件,用于展示大量的数据列表,而“拖拽ListView”则是对这个组件的一个增强功能,允许用户通过手势操作来改变列表中条目的顺序。这种交互方式提高了用户体验,常用于任务...

    可多拽的listview

    "可多拽的listview"(Draggable ListView)是指具有拖放功能的ListView,允许用户通过手势操作来改变列表项的顺序,提供了更加直观和交互性强的用户体验。这种效果在许多应用中都非常实用,比如任务管理器、音乐...

    可以拖动的ListView

    然而,标准的ListView并不支持元素的拖动和排序功能。"可以拖动的ListView"是一个经过增强的ListView实现,它允许用户通过触摸并拖动来改变列表中条目的顺序,提供了更丰富的用户体验。这个Demo提供了一个详细的示例...

    [安卓开源]DragList(可拖拽移动的ListView)

    【标题】"安卓开源DragList(可拖拽移动的ListView)"正是针对这一需求提供的一种解决方案,它允许用户在ListView中自由拖动条目,改变列表的顺序,极大地提升了用户体验。 【描述】"不是固定的列表视图,可以随意的...

    拖动item重新排序的listview

    然而,原生的ListView并不支持直接拖动item来实现重新排序的功能。要实现这个功能,开发者通常需要借助第三方库或者自定义适配器来完成。"拖动item重新排序的listview"这个主题就是关于如何为ListView添加拖放排序...

    可以拖动的listview+限制拖动某一图片

    在Android开发中,ListView是一种常见的组件,用于展示可滚动的列表数据。然而,原生的ListView并不支持元素的拖放操作。本知识点将探讨如何实现一个可以拖动的ListView,并限制用户只对特定图片进行拖动操作。我们...

    ListView 中的item随意拖动毕业设计—(包含完整源码可运行).zip

    然而,原生的ListView并不支持直接的item拖动功能,而这个"ListView 中的item随意拖动毕业设计"项目则是为了解决这个问题,提供了用户可以自由拖动ListView中各个item的交互体验。下面我们将深入探讨这一主题,讲解...

    ListView 中的item随意拖动.zip

    本项目“ListView 中的item随意拖动”就是一个实现这一功能的例子,它可以帮助开发者了解如何在Android应用中实现ListView的拖放排序功能。 首先,我们需要理解ListView的工作原理。ListView通过Adapter来绑定数据...

    android 移动listview

    实现ListView拖拽功能还可以借助一些第三方库,比如`android-draggable-view`或`android-swipe-to-dismiss`。这些库提供了现成的接口和回调,可以简化开发流程,使实现更高效。 7. **性能优化**: 注意ListView的...

    Drag and drop ListView item using jQuery

    为ListView外的某个区域(如其他ListView或容器)设置`droppable()`,使其成为可放置拖动项的区域。当拖动项进入、离开或放置在该区域时,会触发相应的事件。 ```javascript $('#dropArea').droppable({ accept...

    react-drag-listview:一个用于React的简单的可拖动列表组件

    安装例子拖曳行拖动列 发展npm installnpm start 用法const ReactDragListView = require ( 'react-drag-listview' ) ;class Demo extends React . Component { constructor ( props ) { super ( props ) ; const ...

    DragAndDropDemo

    本Demo“DragAndDropDemo”着重展示了如何在ListView中实现这一功能,使得用户可以拖动ListView的项(items)并互换它们的值,从而提供更直观、灵活的界面操作。 首先,要实现拖放功能,我们需要对ListView进行...

    Android应用源码之listviewdrop(可上下拖拽的列表)-IT计算机-毕业设计.zip

    本项目“Android应用源码之listviewdrop(可上下拖拽的列表)”是一个针对毕业设计的学习资源,旨在帮助开发者掌握如何创建一个支持上下拖拽功能的ListView。这个特性使得用户能够通过手势直观地对列表项进行重新排序...

    RecycleView实现内部拖拽排序和两个RecycleView拖拽对调条目的Demo

    为了支持拖拽,`ViewHolder`需要扩展`RecyclerView.ViewHolder`并实现`Draggable`接口,以便在`ItemTouchHelper`中进行操作。 5. **设置监听器**: 创建`ItemTouchHelper`实例并将其与`RecycleView`关联,通过`...

    RevyclerView拖拽排序及侧滑删除的实现

    本教程将详细讲解如何实现`RecyclerView`的拖拽排序以及侧滑删除功能。 首先,我们来讨论拖拽排序。`RecyclerView`本身并不直接支持拖放操作,但我们可以借助`ItemTouchHelper`类来实现这个功能。`ItemTouchHelper`...

    列表拖拽交换和区域删除Demo源码.zip

    在实现拖拽交换功能时,我们需要对RecyclerView进行扩展,添加Draggable功能。这通常涉及到以下步骤: 1. 创建一个DragEvent监听器,用于处理开始拖动、拖动中和拖动结束的事件。 2. 实现OnItemTouchHelperCallback...

Global site tag (gtag.js) - Google Analytics