實現GridView長按拖動圖示,其餘圖示自動移位的效果(仿webos的note風格)
阿新 • • 發佈:2019-01-22
參考文章 :高仿小米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,懶得理它。