Search code examples
androidandroid-recyclerviewshared-element-transitionsuperslim

SharedElements undesirable effect in RecyclerView


I have one activity with one RecyclerView (SuperSlim library) and a detail activity, when I click a item of that list, detail activity will be open. The problem is when I go back, I'm trying to set the clicked element as the first visible element in the list but I get this horrible animation:

enter image description here

This is my onActivityReenter()

    @Override
public void onActivityReenter(int resultCode, Intent data) {
    super.onActivityReenter(resultCode, data);
    Log.d(TAG, "onActivityReenter() called with: resultCode = [" + resultCode + "], data = [" + data + "]");

    mTmpReenterState = new Bundle(data.getExtras());
    int startingPosition = mTmpReenterState.getInt(EXTRA_STARTING_ITEM_POSITION);
    int currentPosition = mTmpReenterState.getInt(EXTRA_CURRENT_ITEM_POSITION);

    mRecycler.scrollToPosition(currentPosition);
    postponeEnterTransition();
    mRecycler.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
        @Override
        public boolean onPreDraw() {
            mRecycler.getViewTreeObserver().removeOnPreDrawListener(this);
            // TODO: figure out why it is necessary to request layout here in order to get a smooth transition.
            mRecycler.requestLayout();
            startPostponedEnterTransition();

            return true;
        }
    });
}

And my SharedElementCallback:

    private final SharedElementCallback exitTransitionCallBack = new SharedElementCallback() {
    @Override
    public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {

        if (mTmpReenterState == null) {
            // If mTmpReenterState is null, then the activity is exiting.
            View navigationBar = findViewById(android.R.id.navigationBarBackground);
            View statusBar = findViewById(android.R.id.statusBarBackground);
            if (navigationBar != null) {
                names.add(navigationBar.getTransitionName());
                sharedElements.put(navigationBar.getTransitionName(), navigationBar);
            }
            if (statusBar != null) {
                names.add(statusBar.getTransitionName());
                sharedElements.put(statusBar.getTransitionName(), statusBar);
            }
        } else {
            int startingPosition = mTmpReenterState.getInt(EXTRA_STARTING_ITEM_POSITION);
            int currentPosition = mTmpReenterState.getInt(EXTRA_CURRENT_ITEM_POSITION);
            if (startingPosition != currentPosition) {
                // If startingPosition != currentPosition the user must have swiped to a
                // different page in the DetailsActivity. We must update the shared element
                // so that the correct one falls into place.
                sharedElements.clear();
                sharedElements.put("number", mLayoutManager.findViewByPosition(currentPosition).findViewById(R.id.text_number));
                sharedElements.put("day", mLayoutManager.findViewByPosition(currentPosition).findViewById(R.id.text_day));
                sharedElements.put("recycler", mLayoutManager.findViewByPosition(currentPosition + 1).findViewById(R.id.recycler));
            }

            mTmpReenterState = null;
        }
    }
};

I think the problem is that the activity try to make an animation from the original item position to the top to the list, but I don't know how avoid that.

enter image description here

Does anyone know how to fix this??

Thanks in advance guys!!!


Solution

  • After a while I've realized that the problem that I was updating the Main Activity list with a notifyItemChanged(int) by LocalBroadcast and the standard recyclerView animation made that glitch. I solve the problem using:

      RecyclerView.setItemAnimator(null) 
    

    because the standard didn't work

      ((SimpleItemAnimator) RecyclerView.getItemAnimator())
                              .setSupportsChangeAnimations(false)
    

    So the problem is nothing to do with SharedElements.