1. 程式人生 > >超順滑!優化android ListView拖拽移位

超順滑!優化android ListView拖拽移位

                                                                                                   

由於專案需求,要實現類似於ios上的listview的排序。之前在網路上搜索,發現了下面這篇文章,先感謝一下:

後來看到魅拍上面也有類似的功能,但是效能要快好幾倍,同時動畫也非常的流暢,於是試著自己優化下。現在效能上基本OK,還有些細節的地方需要優化下,程式碼也需要整

理下。等整理完了,把程式碼放上來供大家參考下。

觸控事件處理:

/**
	 * 觸控事件處理
	 */
	@Override
	public boolean onTouchEvent(MotionEvent ev) {
		// item的view不為空,且獲取的dragPosition有效
		if (dragImageView != null && dragPosition != INVALID_POSITION
				&& !isLock) {
			int action = ev.getAction();
			switch (action) {
			case MotionEvent.ACTION_UP:
				int upY = (int) ev.getY();
				stopDrag();
				onDrop(upY);
				break;
			case MotionEvent.ACTION_MOVE:
				int moveY = (int) ev.getY();
				onDrag(moveY);
				testAnimation(moveY);
				break;
			case MotionEvent.ACTION_DOWN:
				break;
			default:
				break;
			}
			return true;// 取消ListView滑動.
		}

		return super.onTouchEvent(ev);
	}

主要的動畫部分:
private void testAnimation(int y){
		final DragListAdapter adapter = (DragListAdapter) getAdapter();
		int tempPosition = pointToPosition(0, y);
		if (tempPosition == INVALID_POSITION || tempPosition == lastPosition) {
			return;
		}
		mFirstVisiblePosition = getFirstVisiblePosition();
		dragPosition = tempPosition;
		onChangeCopy(lastPosition, dragPosition);
		int MoveNum = tempPosition - lastPosition;
		int count = Math.abs(MoveNum);
		for(int i=1; i<=count; i++){
			int xAbsOffset, yAbsOffset;
			//向下drag
			if(MoveNum > 0){
				if(lastFlag == -1){
					lastFlag = 0;
					isSameDragDirection = true;
				}
				if(lastFlag == 1){
					turnUpPosition = tempPosition;
					lastFlag = 0;
					isSameDragDirection = !isSameDragDirection;
				}
				if(isSameDragDirection){
					holdPosition = lastPosition + 1;
				}else{
					if(startPosition < tempPosition){
						holdPosition = lastPosition + 1;
						isSameDragDirection = !isSameDragDirection;
					}else{
						holdPosition = lastPosition;
					}
				}
				xAbsOffset = 0;
				yAbsOffset = - mItemVerticalSpacing;
				lastPosition++;
			}
			//向上drag
			else{
				if(lastFlag == -1){
					lastFlag = 1;
					isSameDragDirection = true;
				}
				if(lastFlag == 0){
					turnDownPosition = tempPosition;
					lastFlag = 1;
					isSameDragDirection = !isSameDragDirection;
				}
				if(isSameDragDirection){
					holdPosition = lastPosition -1;
				}else{
					if(startPosition > tempPosition){
						holdPosition = lastPosition -1;
						isSameDragDirection = !isSameDragDirection;
					}else{
						holdPosition = lastPosition;
					}
				}
				xAbsOffset = 0;
				yAbsOffset = mItemVerticalSpacing;
				lastPosition--;
			}
			
			adapter.setHeight(mItemVerticalSpacing);
			adapter.setIsSameDragDirection(isSameDragDirection);
			adapter.setLastFlag(lastFlag);
			
		    ViewGroup moveView = (ViewGroup)getChildAt(holdPosition - getFirstVisiblePosition());
		    
		    Animation animation;
		    if(isSameDragDirection){
		    	animation = getFromSelfAnimation(xAbsOffset, yAbsOffset);
		    }else{
		    	animation = getToSelfAnimation(xAbsOffset, -yAbsOffset);
		    }
		    moveView.startAnimation(animation);
		}
	}

動畫部分的變數說明下次補充。