Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //MIT license
- handleViewDatasetChange();
- swapElements(mBackingList, mMobileAdapterPosition, mMobileAdapterPosition + positionChange);
- private static class IdLocator {
- public int left;
- public int top;
- public boolean visible;
- public IdLocator(int left, int top, boolean visible) {
- this.left = left;
- this.top = top;
- this.visible = visible;
- }
- }
- public void handleViewDatasetChange() {
- //THIS MUST BE CALLED PRIOR TO THE UNDERLYING CHANGES IN THE ADAPTER.
- //It saves the state, and calls the notify, and plugs in the predraw listener animations.
- //Save the adapter state.
- final HashMap<Long,IdLocator> locations = new HashMap<>(getChildCount());
- int firstVisiblePosition = getFirstVisiblePosition();
- Adapter adapter = getAdapter();
- for (int i = 0, s = getChildCount(); i < s; i++) {
- View v = getChildAt(i);
- int position = firstVisiblePosition + i;
- long id = adapter.getItemId(position);
- locations.put(id, new IdLocator(v.getLeft(), v.getTop(), v.getVisibility() == View.VISIBLE));
- v.setVisibility(VISIBLE);
- }
- ((BaseAdapter) getAdapter()).notifyDataSetChanged();
- final ViewTreeObserver observer = getViewTreeObserver();
- observer.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
- public boolean onPreDraw() {
- observer.removeOnPreDrawListener(this);
- int firstVisiblePosition = getFirstVisiblePosition();
- Adapter adapter = getAdapter();
- //Animate the changes from the saved states.
- for (int i = 0, s = getChildCount(); i < s; i++) {
- int position = firstVisiblePosition + i;
- long id = adapter.getItemId(position);
- IdLocator idLocator = locations.get(id);
- if (idLocator == null) continue;
- View v = getChildAt(i);
- if (!idLocator.visible) v.setVisibility(INVISIBLE);
- else v.setVisibility(VISIBLE);
- int changeLeft = idLocator.left - v.getLeft();
- if (changeLeft != 0) {
- v.setTranslationX(changeLeft);
- Animator animatorX = ObjectAnimator.ofFloat(v, View.TRANSLATION_X, 0);
- animatorX.setDuration(MOVE_DURATION);
- animatorX.start();
- }
- int changeTop = idLocator.top - v.getTop();
- if (changeTop != 0) {
- v.setTranslationY(changeTop);
- Animator animatorY = ObjectAnimator.ofFloat(v, View.TRANSLATION_Y, 0);
- animatorY.setDuration(MOVE_DURATION);
- animatorY.start();
- }
- }
- return true;
- }
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement