Reputation: 164
i used this guide to make a simple RecyclerView (in a fragment) to Activity Shared Element Transition with multiple elements and it all worked fine up the point i am trying to get back. Althought i pass the ViewHolder as seen below sometimes the return animation ends up on different element on the RecyclerView which is really bad.
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());
Not being able to fix this i decided to actually not use a return animation so in my second activity i replaced
@Override
public void onBackPressed(){
super.onBackPressed();
supportFinishAfterTransition();
}
with
@Override
public void onBackPressed(){
super.onBackPressed();
finish();
overridePendingTransition(android.R.anim.slide_in_left, android.R.anim.slide_out_right);
}
but now when i go back all shared elements are empty!
So, i am trying to figure a way, either to make the return transition work properly, meaning the "fading" effects to fade on the correct item, OR to remove the return transition all together.
Edit: On the direction of actually making it work I tried to obtain and use the View that was clicked instead of the ViewHolder but again, i got the same results. As soon as i was scrolling the recycler view and opening an item, the views would animate back to a row different than the one they started.
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());
}
});
Upvotes: 0
Views: 1716
Reputation: 666
The way you're setting the transitionName in your shared element Pairs won't work. When you return to your calling Activity, the system is looking for a unique view with those transition names, but it isn't finding it. You need to actually call setTransitionName
on your shared Views.
You'd want to set unique transitionNames on each ViewHolder. Consider concatenating the adapterPosition
to the View's transitionName, then pass that position as an extra to your called Activity which sets the transitionName of its Views with that position value.
Upvotes: 1
Reputation: 345
Try calling finish()
without super.onBackPressed()
.
I'm guessing your Activity is an AppCompatActivity? It will execute the pending Transition before finishing.
Upvotes: 3