SharedElements在RecyclerView中产生不良影响

时间:2017-01-23 16:29:52

标签: android android-recyclerview shared-element-transition superslim

我有一个活动,包含一个RecyclerView(SuperSlim库)和一个详细活动,当我点击该列表中的某个项目时,详细活动将会打开。问题是,当我回去时,我正在尝试将被点击的元素设置为列表中的第一个可见元素,但我得到了这个可怕的动画:

enter image description here

这是我的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;
        }
    });
}

我的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;
        }
    }
};

我认为问题在于活动会尝试从原始项目位置到顶部到列表制作动画,但我不知道如何避免这种情况。

enter image description here

有谁知道如何解决这个问题?

先谢谢你们!

1 个答案:

答案 0 :(得分:1)

过了一段时间,我意识到我使用notifyItemChanged(int) LocalBroadcast和标准recyclerView动画更新主活动列表的问题造成了这个问题。我用以下方法解决了这个问题:

  RecyclerView.setItemAnimator(null) 

因为标准不起作用

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

所以问题与SharedElements无关。