实现GridView长按拖动图标,其余图标自动移位的效果(仿webos的note风格)

参考文章 :高仿小米launcher(ZAKER)跨屏拖动item

上述文章是在放在item后,2个位置做一个交换,本文在此基础上,实现了拖动过程中,其它item自动移位的效果。其实就是修改了一下OnMove动作中的执行动画过程以及数据刷新的逻辑,主要还是得益于上述文章。

完整源码下载

主要修改的代码如下:
在onTouchEvent接口的move事件中处理 各个item的移动。代码如下:

@Override
public boolean onTouchEvent(MotionEvent ev) {
		if (dragImageView != null
				&& dragPosition != AdapterView.INVALID_POSITION) {
			int x = (int) ev.getX();
			int y = (int) ev.getY();
			switch (ev.getAction()) {
			case MotionEvent.ACTION_MOVE:
				if(!isCountXY) {
                                    xtox = x-mLastX;
				    ytoy = y-mLastY;
				    isCountXY= true;
				}
				onDrag(x, y);
				if(!isMoving )
				    OnMove(x,y);							
				break;
			case MotionEvent.ACTION_UP:
				stopDrag();
				onDrop(x, y);
				break;
			}
		}
		return super.onTouchEvent(ev);
	}

其中主要的就是onMove(x,y)函数:

	public  void OnMove(int x, int y){
		int TempPosition = pointToPosition(x,y);
		int sOffsetY = specialItemY == -1 ? y - mLastY : y - specialItemY - halfItemWidth;
		int lOffsetY = leftBtmItemY == -1 ? y - mLastY : y - leftBtmItemY - halfItemWidth;
		if(TempPosition != AdapterView.INVALID_POSITION && TempPosition != dragPosition){
			dropPosition = TempPosition;
		}else if(specialPosition != -1 && dragPosition == specialPosition && sOffsetY >= halfItemWidth){
			dropPosition = (itemTotalCount - 1);
		}else if(leftBottomPosition != -1 && dragPosition == leftBottomPosition && lOffsetY >= halfItemWidth){
			dropPosition = (itemTotalCount - 1);
		}	
		if(dragPosition != startPosition)
			dragPosition = startPosition;
		int MoveNum = dropPosition - dragPosition;
		if(dragPosition != startPosition && dragPosition == dropPosition)
			MoveNum = 0;
		if(MoveNum != 0){
			int itemMoveNum = Math.abs(MoveNum);
			float Xoffset,Yoffset;
			for (int i = 0;i < itemMoveNum;i++){
			if(MoveNum > 0){
				holdPosition = dragPosition + 1;
				Xoffset = (dragPosition/nColumns == holdPosition/nColumns) ? (-1) : (nColumns -1);
				Yoffset = (dragPosition/nColumns == holdPosition/nColumns) ? 0 : (-1);
			}else{
				holdPosition = dragPosition - 1;
				Xoffset = (dragPosition/nColumns == holdPosition/nColumns) ? 1 : (-(nColumns-1));
				Yoffset = (dragPosition/nColumns == holdPosition/nColumns) ? 0 : 1;
			}
		    ViewGroup moveView = (ViewGroup)getChildAt(holdPosition);				
			Animation animation = getMoveAnimation(Xoffset,Yoffset);
			moveView.startAnimation(animation);
			dragPosition = holdPosition;
			if(dragPosition == dropPosition)
				LastAnimationID = animation.toString();
			final DateAdapter adapter = (DateAdapter)this.getAdapter();
			animation.setAnimationListener(new Animation.AnimationListener() {
					
				@Override
				public void onAnimationStart(Animation animation) {
						// TODO Auto-generated method stub
					isMoving = true;
				}
					
				@Override
				public void onAnimationRepeat(Animation animation) {
						// TODO Auto-generated method stub
						
				}
					
				@Override
				public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
					String animaionID = animation.toString();
					if(animaionID.equalsIgnoreCase(LastAnimationID)){
						adapter.exchange(startPosition, dropPosition);
						startPosition = dropPosition;
						isMoving = false;	
					}					
				}
			});	
		  }
	   }
	}



主要的思路在链接给出的文章中已经比较清晰了,只是将动画的执行放在了Move的动作中去执行,而非Up的动作中。主要工作量就是以上onMove函数,其它可以看链接文章的代码。



PS:Tab没处理,导致代码格式很难看,NND,懒得理它。

你可能感兴趣的:(Android开发)