1

我有一個活動,其中有一個RecyclerView(SuperSlim庫)和一個詳細活動,當我單擊該列表中的一個項目時,詳細活動將會打開。問題是,當我回去,我試圖設置點擊的元素在列表中的第一個可見元素,但我得到這個可怕的動畫:SharedElements在RecyclerView中產生不良影響

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

過了一段時間,我意識到我更新主要活動列表的問題是notifyItemChanged(int)LocalBroadcast和標準的recyclerView動畫造成的故障。我解決使用問題:

RecyclerView.setItemAnimator(null) 

因爲標準沒有工作

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

所以,問題是無關SharedElements