0

我用this指南做一個簡單的RecyclerView(在一個片段中)活動共享元素過渡與多個元素,它都工作得很好,我想要回到點。儘管我如下所示傳遞ViewHolder,但有時返回動畫會在RecyclerView的不同元素上結束,這非常糟糕。返回到活動正確的過渡或沒有共享元素轉換

public void onPetClicked(PetRecyclerViewAdapter.PetViewHolder holder, int position) { 

      Intent newPostIntent = new Intent(getActivity(), PostDetailsActivity.class); 
      newPostIntent.putExtra("TYPE", mPostType); 
      newPostIntent.putExtra("POSTED_PET", mPetList.get(position)); 

      Pair<View, String> area = Pair.create(holder.itemView.findViewById(R.id.removable), "content_area"); 
      Pair<View, String> p1 = Pair.create(holder.itemView.findViewById(R.id.ivPetImage), "petImage"); 
      Pair<View, String> p2 = Pair.create(holder.itemView.findViewById(R.id.tvTitle), "postTitle"); 
      Pair<View, String> p3 = Pair.create(holder.itemView.findViewById(R.id.tvInfo), "postDescription"); 
      Pair<View, String> p4 = Pair.create(holder.itemView.findViewById(R.id.rlDate), "postDate"); 
      Pair<View, String> p5 = Pair.create(holder.itemView.findViewById(R.id.rlLocation), "postLocation"); 
      ActivityOptionsCompat options = ActivityOptionsCompat. 
        makeSceneTransitionAnimation(getActivity(), area, p1, p2, p3, p4,p5); 

      getActivity().startActivity(newPostIntent, options.toBundle()); 

不能夠解決這個問題,我決定其實並不使用返回動畫所以在我的第二個活動我更換

@Override 
    public void onBackPressed(){ 
     super.onBackPressed(); 
     supportFinishAfterTransition(); 
    } 

@Override 
    public void onBackPressed(){ 
     super.onBackPressed(); 
     finish(); 
     overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right); 
    } 

但現在,當我回去所有共享元素都是空的!

所以,我試圖想出一個辦法,要麼使返回轉換正常工作,這意味着淡入正確的項目,「褪色」的影響,或刪除一起返回轉換。

編輯:關於實際使它工作的方向,我嘗試獲取和使用被點擊的視圖而不是ViewHolder,但是我又得到了相同的結果。只要我滾動回收站視圖並打開一個項目,視圖就會回到與他們開始的行不同的行。

public void onPetClicked(View view, PetRecyclerViewAdapter.PetViewHolder holder, int position) { 
      mPetList.get(position); 

      Intent newPostIntent = new Intent(getActivity(), PostDetailsActivity.class); 
      newPostIntent.putExtra("TYPE", mPostType); 
      newPostIntent.putExtra("POSTED_PET", mPetList.get(position)); 

      Pair<View, String> area = Pair.create(view.findViewById(R.id.removable), "content_area"); 
      Pair<View, String> p1 = Pair.create(view.findViewById(R.id.ivPetImage), "petImage"); 
      Pair<View, String> p2 = Pair.create(view.findViewById(R.id.tvTitle), "postTitle"); 
      Pair<View, String> p3 = Pair.create(view.findViewById(R.id.tvInfo), "postDescription"); 
      Pair<View, String> p4 = Pair.create(view.findViewById(R.id.rlDate), "postDate"); 
      Pair<View, String> p5 = Pair.create(view.findViewById(R.id.rlLocation), "postLocation"); 
      ActivityOptionsCompat options = ActivityOptionsCompat. 
        makeSceneTransitionAnimation(getActivity(), area, p1, p2, p3, p4,p5); 

      getActivity().startActivity(newPostIntent, options.toBundle()); 
     } 
    }); 

回答

2

嘗試調用finish()沒有super.onBackPressed()

我猜你的Activity是一個AppCompatActivity?它將在完成之前執行未完成的轉換。

+0

好趕!謝謝 – eiran