Android实现可拖拽的ListView
转载自:http://www.open-open.com/lib/view/open1346147512317.html
通过继承ListView实现可拖拽的ListView,先说说实现拖拽的原理吧,实现拖拽需要考虑三个问题:第一怎么确定你在拖拽listview里面的item的时候就是你手指当前选中的item;第二实现拖拽的效果,就是有一个浮动的层跟随你的手指在移动;第三你放开手指时怎么把你拖拽的这个item放到当前listView的位置(也就是说改变item的位置)。明白了这三个问题就比较好实现了。
里面会涉及到一些比较重要的方法调用,首先是pointToPosition(int x, int y)这方方法Android?官方的解释是” Maps a point to a position in the list”,我把它理解为通过x和y的位置来确定这个listView里面这个item的位置。有了这个方法就解决了第一和第三个问题了。接下来我们可以通过WindowManager来解决第二个问题,然后通过pointToPosition方法就可以获取你手指按下时的item,这个item其实就是你listview里面的item了,这样的就可以把这个item设置为是WindowManager的view,这样的话拖动的层的效果就模拟出来了,接下来是怎么让这个WindowManager跟随你的手指在移动。这个时候会涉及到WindowManager里面的updateViewLayout(view, layoutparams)来刷新WindowManager的位置,这样就实现了WindowManager会跟随你的手指在移动。最后就剩下你放下手指的时候怎么让你拖拽的item插入到listview里面,这个插入的动作其实包含了移除和插入这两个动作。这个时候你可能会问在某个位置插入这个item需要”position”和”item”两个参数,position我们可以通过pointToPosition方法来获取,然后要插入的“item”其实是你adapter里面数据。因为我们上面的一系列动作都是在listview里面完成的,但是在我们重写listview的时候是还没有给listview设置adapter是吧,这个问题的我们通过在重写listview的类中自定义一个接口,然后你在activity里面初始化listview数据的时候实现这个接口。接口里面只有一个方法,方法里面的两个参数一个是你开始拖拽的的item的位置,另一个是你拖拽移动之后之后的item的位置。下面我们看看效果吧:


下面我们看看代码:
?
01????????private?final?float?mAlpha =?0.9f;02????//拖动的view03????private?ImageView mDragView;04????private?Context mContext;05????private?WindowManager mWindowManager;06????private?WindowManager.LayoutParams mLayoutParams;07????//开始拖动时的位置08????private?int?mDragStartPosition;09????//当前的位置10????private?int?mDragCurrentPostion;11????//在滑动的时候,手的移动要大于这个返回的距离值才开始移动控件12????private?int?mScaledTouchSlop;13????//当前位置距离边界的位置14????private?int?mDragOffsetX;15????private?int?mDragOffSetY;16????//移动的位置17????private?int?mDragPointX;18????private?int?mDragPointY;19????//边界20????private?int?mUpperBound;21????????private?int?mLowerBound;22????private?DropViewListener mDropViewListener;23?????24????public?DragListView(Context context) {25????????super(context);26????????mContext = context;27????????mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);28????????mScaledTouchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();29????}30?31????public?DragListView(Context context, AttributeSet attr) {32????????super(context, attr);33????????mContext = context;34????????mWindowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);35????????mScaledTouchSlop = ViewConfiguration.get(mContext).getScaledTouchSlop();36????}?
我们在onInterceptTouchEvent方法里面作初始化动作:
?
01@Override02????public?boolean?onInterceptTouchEvent(MotionEvent ev) {03????????//ev.getX()相对于控件本身左上角,ev.getRawX()相对于容器圆点位置04????????switch?(ev.getAction()) {05????????????case?MotionEvent.ACTION_DOWN:06????????????????final?int?x = (int) ev.getX();//相对于空间本身07????????????????final?int?y = (int) ev.getY();08????????????????final?int?itemNum = pointToPosition(x, y);09????????????????if(itemNum == AdapterView.INVALID_POSITION){10????????????????????break;11????????????????}12????????????????final?ViewGroup item = (ViewGroup) getChildAt(itemNum - getFirstVisiblePosition());13????????????????mDragPointX = x - item.getLeft();14????????????????mDragPointY = y - item.getTop();15????????????????mDragOffsetX = ((int) ev.getRawX()) - x;16????????????????mDragOffSetY = ((int) ev.getRawY()) - y;17?????????????????18????????????????//长按19????????????????item.setOnLongClickListener(new?OnLongClickListener() {20????????????????????@Override21????????????????????public?boolean?onLongClick(View v) {22????????????????????????//计算边界23????????????????????????final?int?height = getHeight();24????????????????????????mUpperBound = Math.min(y - mScaledTouchSlop, height /?3);25????????????????????????mLowerBound = Math.max(y + mScaledTouchSlop, height *?2?/?3);26????????????????????????mDragCurrentPostion = mDragStartPosition = itemNum;27?????????????????????????28????????????????????????item.setDrawingCacheEnabled(true);29????????????????????????Bitmap bitmap = Bitmap.createBitmap(item.getDrawingCache());30????????????????????????startDragging(bitmap, x, y);31????????????????????????return?true;32????????????????????}33????????????????});34????????????????break;35????????}36????????return?super.onInterceptTouchEvent(ev);37????}?
这里面我们就记录了拖拽的当前和开始时位置mDragCurrentPostion和 mDragStartPosition,并通过startDragging来初始化mWindowManager,在startDragging方法里面调用的stopDragging方法其实是一个内存释放的过程,这个方法做的事情就是释放内存空间。
?
?
01private?void?startDragging(Bitmap bitm,?int?x,?int?y){02????????stopDragging();03?????????04????????mLayoutParams =?new?WindowManager.LayoutParams();05????????mLayoutParams.gravity = Gravity.TOP | Gravity.LEFT;06????????mLayoutParams.x = x - mDragPointX + mDragOffsetX;07????????mLayoutParams.y = y - mDragPointY + mDragOffSetY;08????????mLayoutParams.width = WindowManager.LayoutParams.WRAP_CONTENT;09????????mLayoutParams.height = WindowManager.LayoutParams.WRAP_CONTENT;10????????mLayoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE11????????????????| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE12????????????????| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON13????????????????| WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN14????????????????| WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;15????????mLayoutParams.format = PixelFormat.TRANSLUCENT;16????????mLayoutParams.windowAnimations =?0;17?????????18????????ImageView imageView =?new?ImageView(mContext);19????????imageView.setImageBitmap(bitm);20????????imageView.setBackgroundResource(R.drawable.tab_item_bg);21????????imageView.setPadding(0,?0,?0,?0);22????????mWindowManager.addView(imageView, mLayoutParams);23????????mDragView = imageView;24????}25?26private?void?stopDragging(){27????????if(mDragView !=?null){28????????????mWindowManager.removeView(mDragView);29????????????mDragView.setImageDrawable(null);30????????????mDragView =?null;31????????}32????}?
我们在onInterceptTouchEvent方法记录了拖拽的开始位置和当前位置,并且初始化了windowmanager,接下来我们通过onTouchEvent方法来实现让windowmanager跟随你的手指移动
?
?
01@Override02????public?boolean?onTouchEvent(MotionEvent ev) {03????????if(mDragView !=?null?&& mDragCurrentPostion != INVALID_POSITION && mDropViewListener !=?null){04????????????switch?(ev.getAction()) {05????????????????case?MotionEvent.ACTION_UP:06????????????????????//int y = (int) ev.getY();07????????????????????stopDragging();08????????????????????//数据交换09????????????????????if(mDragCurrentPostion >=?0?&& mDragCurrentPostion < getCount()){10????????????????????????mDropViewListener.drop(mDragStartPosition, mDragCurrentPostion);11????????????????????}12????????????????????break;13????????????????case?MotionEvent.ACTION_MOVE:14????????????????????int?x = (int) ev.getX();15????????????????????int?y = (int) ev.getY();16????????????????????dragView(x, y);17????????????????????if?(y >= getHeight() /?3) {18????????????????????????mUpperBound = getHeight() /?3;19????????????????????}20????????????????????if?(y <= getHeight() *?2?/?3) {21????????????????????????mLowerBound = getHeight() *?2?/?3;22????????????????????}23?????????????????????int?speed =?0;24????????????????????if?(y > mLowerBound) {25????????????????????????if?(getLastVisiblePosition() < getCount() -?1) {26????????????????????????????speed = y > (getHeight() + mLowerBound) /?2???16?:?4;27????????????????????????}?else?{28????????????????????????????speed =?1;29????????????????????????}30????????????????????}?else?if?(y < mUpperBound) {31????????????????????????speed = y < mUpperBound /?2?? -16?: -4;32????????????????????????if?(getFirstVisiblePosition() ==?033????????????????????????????????&& getChildAt(0).getTop() >= getPaddingTop()) {34????????????????????????????speed =?0;35????????????????????????}36????????????????????}37????????????????????if?(speed !=?0) {38????????????????????????smoothScrollBy(speed,?30);39????????????????????}40????????????????????break;41????????????}42????????????return?true;43????????}44????????return?super.onTouchEvent(ev);45????}?
其中dragView方法就是根据你手指移动来改变windowmanager的位置,在移动的时候我们需要考虑的另外一个问题是你的listview滚动条的时候,这个时候我们需要考虑边界,不然会抛出空指针异常。
?
?
01private?void?dragView(int?x,?int?y){02????????if(mDragView !=?null){03????????????mLayoutParams.alpha = mAlpha;04????????????mLayoutParams.y = y - mDragPointY + mDragOffSetY;05????????????mLayoutParams.x = x - mDragPointX + mDragOffsetX;06????????????mWindowManager.updateViewLayout(mDragView, mLayoutParams);07????????}08????????int?tempPosition = pointToPosition(0, y);09????????if(tempPosition != INVALID_POSITION){10????????????mDragCurrentPostion = tempPosition;11????????}12?????????13????????//滚动14????????int?scrollY =?0;15????????if(y < mUpperBound){16????????????scrollY =?8;17????????}else?if(y > mLowerBound){18????????????scrollY = -8;19????????}20?????????21????????if(scrollY !=?0){22????????????int?top = getChildAt(mDragCurrentPostion - getFirstVisiblePosition()).getTop();23????????????setSelectionFromTop(mDragCurrentPostion, top + scrollY);24????????}25????}?
?这样的话就完成了拖拽的效果,剩下的是你放开手指,然后把你拖拽的item插入到listview的列表里面,我们定义了一个接口
?
?
1public?void?setDropViewListener(DropViewListener listener){2????????this.mDropViewListener = listener;3????}4?????5????public?interface?DropViewListener {6????????void?drop(int?from,?int?to);7????}?
这样的话,你在初始化listview给listView设置adapter的时候需要实现DropViewListener接口,from和to两个参数分别是你拖拽时的最初位置和你移动后的位置,这样的话你在activity里面就可以实现数据插入了。
?
注意:
但是如果你的这listview的item包含了复选框的话,这个时候listview的onitemclicklistener事件就会失效,也就是说你不能通过你平时处理listview的onitemclicklistener的方法一样处理,你也可以通过自定义接口来模拟onitemclicklistener事件。