Custom shared element transition in end activity
Asked Answered
H

2

6

I have list of feed data in global application. I used this code to make a transition from my RecyclerView in MainAcitivty

// selected item event
Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("item_feed_index", selectedItemIndex);
FeedViewHolder viewHolder = getViewHolderFromPosition(position);
Pair<View, String> pair1 = Pair.create(viewHolder.imageView, "TransitionName.Feed.Image");
Pair<View, String> pair2 = Pair.create(viewHolder.textView, "TransitionName.Feed.Text");
Pair<View, String> pair3 = Pair.create(viewHolder.button, "TransitionName.Feed.Button");
ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, pair1, pair2, pair3);
startActivity(intent, options.toBundle());

It does work. But the problem is that in DetailActivity, I have another vertical list, which user can scroll down/up to change the Feed item in list (which change the scroll index as well).

When press back from DetailActivity, I want the exit animation transition execute on correct viewing feed index. How can I do that?

This is the code onActivityResult in MainActivity, it scroll the recycle to correct position, but the exit transition animation isn't correct position.

int currentFeedIndex = Application.getInstance().getCurrentViewingFeed();
if (currentFeedIndex > 0 && currentFeedIndex < adapter.getItemCount()) {
   if (gridLayoutManager != null) {
      gridLayoutManager.scrollToPositionWithOffset(currentFeedIndex + 1, 0);
   }
}

I'm looking for the way to change the pair list before running exit animation

Update: here is the layout of DetailActivity, where I assign transition names to views

 <RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:id="@+id/imgFeed"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:transitionName="TransitionName.Feed.Image" />

    <TextView
        android:id="@+id/tvLogo"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:transitionName="TransitionName.Feed.Text"/>

    <Button
        android:transitionName="TransitionName.Feed.Button"
        android:id="@+id/btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:layout_below="@+id/tvLogo"/>
    </RelativeLayout>
Holster answered 7/7, 2016 at 2:57 Comment(2)
Could you post the code how did you assign transition names to views of the DetailActivity ?Patter
@rom4ek I updated codeHolster
A
2

On caller activity:

String itemId; //set your item id here

Intent intent = new Intent(this, DetailActivity.class);
intent.putExtra("item_feed_index", selectedItemIndex);
intent.putExtra("item_id", itemId);

String imageViewTransitionName = "TransitionName.Feed.Image." + itemId;
String textViewTransitionName = "TransitionName.Feed.Text." + itemId;
String buttonTransitionName = "TransitionName.Feed.Button." + itemId;

ViewCompat.setTransitionName(viewHolder.imageView, imageViewTransitionName);
ViewCompat.setTransitionName(viewHolder.textView, textViewTransitionName);
ViewCompat.setTransitionName(viewHolder.button, buttonTransitionName);

Pair<View, String> pair1 = Pair.create(viewHolder.imageView, imageViewTransitionName);
Pair<View, String> pair2 = Pair.create(viewHolder.textView, textViewTransitionName);
Pair<View, String> pair3 = Pair.create(viewHolder.button, buttonTransitionName);

ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(this, pair1, pair2, pair3);
startActivity(intent, options.toBundle());

Then in your destination Activity you just need to get itemId from Intent and set correct transition name for each view.

String itemId = getIntent().getString("item_id");
String imageViewTransitionName = "TransitionName.Feed.Image." + itemId;
String textViewTransitionName = "TransitionName.Feed.Text." + itemId;
String buttonTransitionName = "TransitionName.Feed.Button." + itemId;

ViewCompat.setTransitionName(findViewById(R.id.imageView), imageViewTransitionName);
ViewCompat.setTransitionName(findViewById(R.id.textView), textViewTransitionName);
ViewCompat.setTransitionName(findViewById(R.id.button), buttonTransitionName);
Ameline answered 12/7, 2016 at 17:21 Comment(2)
You don't get the point.Enter animation from MainActivity to DetailActivity is correct with those transition names. But in DetailActivity, there has a viewpager to switch to another item (which changes the itemId). What I want is to update the new itemId for exit animation when press back from DetailActivity to MainActivityHolster
@R4j maybe you could post a small video explaining your issue, so we can get you finally :)Patter
G
1

Override the onBackPressed method on your DetailActivity like this:

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

Also, make sure you have specified the exit transition on your styles.xml

Giamo answered 13/7, 2016 at 8:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.