Android - Recyclerview shared element transition item position
Asked Answered
U

6

16

I hope you could help me with this issue. I have two fragments, the first one is a recyclerview with several images, the second fragment is the details view of this images. If user clicks on an image the app does a fragment transaction and the details are displayed.

I have implemented a shared element transition between the fragments successfully, if I click the little image on the 1st fragment it gets bigger and moves to its final position on the details view.

Well then, here's the issue, the initial position of the image is not the expected, it starts moving a few pixels from its original position, when I click the image jumps a little bit to the right and to the bottom.

Why is this happening? its annoying!

The transition xml:

<transitionSet xmlns:android="http://schemas.android.com/apk/res/android">
    <changeTransform />
    <changeBounds />
</transitionSet>

If I do the same for a button outside the recyclerview it works perfectly. Is this some kind of bug in the recyclerview?

Unscramble answered 24/2, 2015 at 11:28 Comment(3)
would also like clarification on this as well, as I am running into a similar problem todayRupert
@Rupert If you find how to fix it please let us knowUnscramble
Can you post any relevant code you are using? A video illustrating the problem might help as well (Android Studio's screen capture feature can come in handy here).Maiolica
C
15

Your RecyclerView items need to set a unique transition name on their shared view. If you imagine the rendered layout hierarchy of your RecyclerView, there will be a lot of items (views) in it that have the same transition name. This way it is unclear for the transition, which the actual shared element is. For example, you could append the position to the views transition name, e.g. transition1, transition2, etc. Now when you start your detail fragment, you have to hand over the transition name to it and set this name to the shared view in your detail fragment, e.g. in onViewCreated().

Chunchung answered 26/6, 2015 at 11:31 Comment(0)
C
7

This could happen if the correct view is not being picked up from your recycler view. Make sure that you use the precise view (based on the position) instead of the root view. For e.g. in the below snippet the View v (which you receive in the onClick()) should be used:

mAdapter.setOnItemClickListener(

                new MyAdapter.OnItemClickListener() {

                    @Override
                    public void onClick(View v, int position) {

                        ImageView heroView;
                        heroView = (ImageView) v.findViewById(R.id.category_icon);
                        ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation(
                                getActivity(), heroView, heroView.getTransitionName());
                        ActivityCompat.startActivity(getActivity(), intent, options.toBundle());
Counterpart answered 22/1, 2016 at 13:51 Comment(0)
M
2

If your shared elements are ImageViews, then you need to use a ChangeImageTransform transition as well. Try adding <changeImageTransform /> to your transition set.

Maiolica answered 25/2, 2015 at 3:11 Comment(1)
Thanks for the help, but I've tried a lot of transition combinations and still the same result.Unscramble
M
1

I solved this problem by following this article Shared Element Transitions - Part 4: RecyclerView, which shows codes snips and give detailed explanation.

make sure the android:transitionName between shared elements must be the same, and also has to be unique in the view hierarchy.

As for RecyclerView to Fragment, If we set the transitionName in XML then all ImageView in the gallery would have the same one, which means when we came back to the gallery the framework would have no idea where to move the image to.

Since the item position is unique, so it's best to use position here to set transitionName in onBindViewHolder.

Mysia answered 4/7, 2017 at 17:12 Comment(0)
E
0

For anyone else having these issues, I solved it by making sure the same image transformation was applied to both shared elements. In my case I had no transformation applied to my fragment2 and Centre Crop applied to my fragment1, causing the visual bug.

Epizootic answered 28/10, 2018 at 12:0 Comment(0)
P
0

You can delete item name="android:windowFullscreen"true in the style->theme.its useful to me.

Punchboard answered 3/7, 2021 at 1:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.