I would like to implement a news app with shared element transitions between a RecyclerView and a Viewpager. My app has the following structure:
- Activity1 displays a
RecyclerView
. Each element of this list contains anImageView
(the shared element item) and a title of a news. - Activity2 display the detail page of this news with a big image (the opposite shared element item)
- When clicking one of the news items in Activity1 the shared element transition should start and expand the image to the big image, displayed in Activity2.
Up to this point, there's no problem. The image expands at the EnterTransition
and minimize at ReturnTransition
. Now the complicated point. I would like to implement a ViewPager
into Activity2, so that the user is able to swipe between the news, that are shown in the ticker.
After I have implemented the Viewpager the EnterTransition still works fine, but now the ReturnTransition is broken. This broken transition is caused in the following situation:
- User presses ticker element 1 (or any other), shared element transition to Activity2 is started.
- Activity2 is displayed. User swipes through news, till position 20 and presses the back button.
- Obviously the shared element image at position 20 is currently not visible in the recyclerview, so the ReturnTransition
is not working.
I tried to set the current position of the recyclerview in the onActivityReenter of Activity1. If I now return from Activity2 the last visited news element is the first visible item in the recyclerview, but the ReturnTransition with the shared element still isn't working.
Anyone got an idea how to get rid of this problem? Below you find my code:
OnActivityReenter in Activity1
@Override
public void onActivityReenter(int requestCode, Intent data) {
super.onActivityReenter(requestCode, data);
if(requestCode == Constants.NEWSDETAIL_ACTIVITY_RESULT_OK) {
mIsReentering = true;
mTransitionBundle = new Bundle(data.getExtras());
int oldPosition = mTransitionBundle.getInt(Constants.ACTIVITY_TRANSITION_OLD_ITEM_POS);
int currentPosition = mTransitionBundle.getInt(Constants.ACTIVITY_TRANSITION_CURRENT_ITEM_POS);
if(oldPosition != currentPosition) {
//Scrolls to new position in the recyclerview
scrollToNews(currentPosition + 1);
}
}
}
FinishAfterTransition in Activity2
@Override
public void finishAfterTransition() {
mIsReturning = true;
if(BasicHelper.isLollipop()) {
getWindow().setReturnTransition(new Slide());
}
int currentPosition = -1;
//gets current position of viewpager
if(currentlyAttached instanceof NewsDetailViewpagerFragment) {
currentPosition = ((NewsDetailViewpagerFragment) currentlyAttached).getCurrentPosition();
}
Intent data = new Intent();
data.putExtra(Constants.ACTIVITY_TRANSITION_OLD_ITEM_POS, getIntent().getExtras().
getInt(Constants.ACTIVITY_TRANSITION_CURRENT_ITEM_POS));
data.putExtra(Constants.ACTIVITY_TRANSITION_CURRENT_ITEM_POS, currentPosition);
setResult(Constants.NEWSDETAIL_ACTIVITY_RESULT_OK, data);
super.finishAfterTransition();
}
If you need more code let me know. Thanks for your help.