private void scrollToPosition(final int position) { DragAndDropListView list = dragAndDropListView; if (list.getFirstVisiblePosition() < position && position < list.getLastVisiblePosition()) { return; } list.setIsScrolling(); if (position <= list.getFirstVisiblePosition()) { list.smoothScrollToPosition(0, position + 2); } else { list.smoothScrollToPosition(brickList.size() - 1, position - 2); } }
@Override public void drop() { int to = toEndDrag; if (to < 0 || to >= brickList.size()) { to = brickList.size() - 1; } if (retryScriptDragging || to != getNewPositionForScriptBrick(to, draggedBrick)) { scrollToPosition(dragTargetPosition); draggedBrick = null; initInsertedBrick = true; positionOfInsertedBrick = dragTargetPosition; notifyDataSetChanged(); retryScriptDragging = false; return; } int tempTo = getNewPositionIfEndingBrickIsThere(to, draggedBrick); if (to != tempTo) { to = tempTo; } if (addingNewBrick) { if (draggedBrick instanceof ScriptBrick) { addScriptToProject(to, (ScriptBrick) draggedBrick); } else { if (script != null) { addBrickToPositionInUserScript(to, draggedBrick); } else { addBrickToPositionInProject(to, draggedBrick); } } if (draggedBrick instanceof UserBrick) { int positionOfUserbrickInScript = 0; for (int index = 0; index <= to; index++) { if (brickList.get(index) instanceof UserBrick && ((UserBrick) brickList.get(index)).getUserBrickId() == ((UserBrick) draggedBrick).getUserBrickId()) { positionOfUserbrickInScript++; } } for (int parameterIndex = 0; parameterIndex < ProjectManager.getInstance() .getCurrentProject() .getUserVariables() .getOrCreateVariableListForUserBrick( ((UserBrick) draggedBrick).getUserBrickId()) .size(); parameterIndex++) { ((UserBrick) draggedBrick) .addUserBrickPositionToParameter( Pair.create(positionOfUserbrickInScript, parameterIndex)); } ArrayList<Pair<Integer, Integer>> userBrickPositionToParameterList = ((UserBrick) draggedBrick).getUserBrickPositionToParameter(); int numberOfUserBricksInScript = userBrickPositionToParameterList.size(); int frequencyOfEqualFirstParameters = 0; for (int newIndex = 0; newIndex < userBrickPositionToParameterList.size(); newIndex++) { if (userBrickPositionToParameterList.get(newIndex).first == userBrickPositionToParameterList.get(numberOfUserBricksInScript - 1).first) { frequencyOfEqualFirstParameters++; } } if (frequencyOfEqualFirstParameters != ProjectManager.getInstance() .getCurrentProject() .getUserVariables() .getOrCreateVariableListForUserBrick(((UserBrick) draggedBrick).getUserBrickId()) .size()) { for (int userBrickPosition = positionOfUserbrickInScript; userBrickPosition < numberOfUserBricksInScript; userBrickPosition++) { Pair<Integer, Integer> userBrickPositionToParameter = ((UserBrick) draggedBrick).getUserBrickPositionToParameter().get(userBrickPosition); if (userBrickPositionToParameter.first >= userBrickPosition) { ((UserBrick) draggedBrick) .setUserBrickPositionToParameter( Pair.create( userBrickPositionToParameter.first + 1, userBrickPositionToParameter.second), ((UserBrick) draggedBrick) .getUserBrickIndexInScript(userBrickPositionToParameter)); } } } // TODO: test if everything gets updated if userbrick gets moved (should work) and delete // arraylist entry when userbrick gets deleted, when new variable gets created update } addingNewBrick = false; } else { if (script != null) { moveUserBrick(fromBeginDrag, toEndDrag); } else { moveExistingProjectBrick(fromBeginDrag, toEndDrag); } } draggedBrick = null; firstDrag = true; initBrickList(); notifyDataSetChanged(); int scrollTo = to; if (scrollTo >= brickList.size() - 1) { scrollTo = getCount() - 1; } dragAndDropListView.smoothScrollToPosition(scrollTo); }