shared element transaction not working with recyclerview when more than one element

421 views Asked by At

there is a recyclerview in android application and i want shared element transaction when user goes to other fragment for that I tried as mentioned below code.

image view in row xml file

 <ImageView
    android:id="@+id/iv_album_thumbnail"
    android:layout_width="match_parent"
    android:layout_height="160dip"
    android:layout_gravity="center"
    android:transitionName="image_transit"
    android:scaleType="centerCrop"
    tools:ignore="ContentDescription"
    android:src="@drawable/img_default"
    android:gravity="center" />

image view in second fragment

<ImageView
    android:id='@+id/iv_album_thumbnail'
    android:layout_width="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_height="wrap_content"
    android:transitionName="image_transit"
    tools:ignore="ContentDescription,UnusedAttribute"
    android:layout_marginTop="60dip"
    android:layout_alignParentTop="true"
    android:src="@drawable/placeholder" />

on click method

public void onAlbumClicked(RecyclerViewAdapter.ViewHolder holder, int position) {

    SongListFragment kittenDetails = SongListFragment.newInstance(aList.get(position));

    // Note that we need the API version check here because the actual transition classes (e.g. Fade)
    // are not in the support library and are only available in API 21+. The methods we are calling on the Fragment
    // ARE available in the support library (though they don't do anything on API < 21)
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        kittenDetails.setSharedElementEnterTransition(new DetailsTransition());
        kittenDetails.setEnterTransition(new Fade());
        setExitTransition(new Fade());
        kittenDetails.setSharedElementReturnTransition(new DetailsTransition());
    }

    getActivity().getSupportFragmentManager()
            .beginTransaction()
            .addSharedElement(holder.imgPicture, "image_transit")
            .setCustomAnimations(R.anim.fade_in, R.anim.fade_out)
            .replace(R.id.root, kittenDetails)
            .addToBackStack(null)
            .commit();
}

when there is only one item in recyclerview it is working perfect but when there is more than one item in recyclerview there is no effect at all, can anyone please currant me.

1

There are 1 answers

2
bwt On BEST ANSWER

I think the problem is that you have multiple views having the same transition name in the source view tree.

You should set it only on clicked row :

  • remove the android:transitionName attribute from the row xml.
  • add ViewCompat.SetTransitionName(holder.imgPicture, "image_transit");