An app I\'m developing displays a grid of images. When you tap an image, it goes into the details view. The details view contains a ViewPager that allows you swipe between e
From what I can tell (and correct me if I'm wrong), what you are trying to achieve is basically something like this: Assume you have a MainActivity
, a DetailsActivity
, and an arbitrary set of images. The MainActivity
displays the set of images in a grid and the DetailsActivity
displays the same set of images in a horizontal ViewPager
. When the user selects an image in the MainActivity
, that image should transition from its position in the grid to the correct page in the second activity's ViewPager
.
The problem we want to solve is "what if the user switches pages inside the DetailsActivity
"? If this happens, we want to change the shared image that will be used during the return transition. By default, the activity transition framework will use the shared element that was used during the enter transition... but the view pager's page has changed so obviously we want to somehow override this behavior. To do so, we will need to set a SharedElementCallback
in your MainActivity
and DetailsActivity
's onCreate()
methods and override the onMapSharedElements()
method like so:
private final SharedElementCallback mCallback = new SharedElementCallback() {
@Override
public void onMapSharedElements(List names, Map sharedElements) {
if (mCurrentImagePosition != mOriginalImagePosition) {
View sharedView = getImageAtPosition(mCurrentImagePosition);
names.clear();
sharedElements.clear();
names.add(sharedView.getTransitionName());
sharedElements.put(sharedView.getTransitionName(), sharedView);
}
}
/**
* Returns the image of interest to be used as the entering/returning
* shared element during the activity transition.
*/
private View getImageAtPosition(int position) {
// ...
}
};
For a more complete solution, I have created a sample project on GitHub that will achieve this effect (there is too much code to post in a single StackOverflow answer).