title: Android shared element transition between fragments having recyclerview & details view
body: <p>I have 2 fragments: fragment 1 containing recyclerview &amp; fragment 2 having detail view for selected item. There is a shared element imageview between these 2 fragments. While transitioning from fragment 1 to 2 I want to see standard shared element transition effect, but it doesn't seem to work with recyclerview. Because if I use just another separate view in 1st fragment instead of recyclerview, it exhibits proper transition. Note that I have assigned position based unique transition names to recyclerview item images. </p>
  
  <p>Code is as follows:
  MyActivity.java</p>
  
  <pre><code>public class MyActivity extends Activity {
  
  private static final String TAG = MyActivity.class.getSimpleName();
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_my);
      if (savedInstanceState == null) {
          getFragmentManager().beginTransaction()
                  .add(R.id.main_container, new PlaceholderFragment())
                  .commit();
      }
  }
  
  public static class ImageAdapter extends RecyclerView.Adapter&lt;ImageAdapter.ViewHolder&gt; {
      public interface OnItemClickListener {
          void onItemClick(View view, int position);
      }
  
      private OnItemClickListener mItemClickListener;
  
      List&lt;Integer&gt; items = new ArrayList&lt;Integer&gt;();
      public void setOnItemClickListener(OnItemClickListener listener) {
          this.mItemClickListener = listener;
      }
  
  
      public ImageAdapter() {
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
          items.add(1);
      }
  
      @Override
      public ImageAdapter.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
          View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, null);
          ViewHolder viewHolder = new ViewHolder(view);
          return viewHolder;
      }
  
      @Override
      public void onBindViewHolder(ViewHolder viewHolder, final int i) {
          viewHolder.container.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View view) {
                  if (mItemClickListener != null) {
                      mItemClickListener.onItemClick(view, i);
                  }
              }
          });
          viewHolder.imgDummy.setTransitionName("test" + i);
      }
  
      @Override
      public int getItemCount() {
          return items.size();
      }
  
      public static class ViewHolder extends RecyclerView.ViewHolder {
          public View container;
          public View imgDummy;
  
          public ViewHolder(View itemView) {
              super(itemView);
              container = itemView.findViewById(R.id.container);
              imgDummy = itemView.findViewById(R.id.imgDummy);
          }
      }
  }
  /**
   * A placeholder fragment containing a simple view.
   */
  @SuppressLint("ValidFragment")
  public class PlaceholderFragment extends Fragment {
  
      private RelativeLayout.LayoutParams lp;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.fragment_my, container, false);
          final View imgDummy2 = rootView.findViewById(R.id.imgDummy2);
          imgDummy2.setTransitionName("test");
  
          RecyclerView list = (RecyclerView) rootView.findViewById(R.id.streams_list);
          list.setLayoutManager(new LinearLayoutManager(getActivity()));
          ImageAdapter adapter = new ImageAdapter();
          adapter.setOnItemClickListener(new ImageAdapter.OnItemClickListener() {
              @Override
              public void onItemClick(View view, int position) {                  
                  View imgDummy = view.findViewById(R.id.imgDummy);
  
                  SecondFragment secondFragment = new SecondFragment();
                  secondFragment.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.trans_move));
  
                  secondFragment.setId(imgDummy.getTransitionName());
                  android.app.FragmentTransaction trans = getFragmentManager().beginTransaction();
                  trans.replace(R.id.main_container, secondFragment);
                  trans.addToBackStack(null);
                  trans.addSharedElement(imgDummy, imgDummy.getTransitionName());
                  trans.commit();
              }
          });
          list.setAdapter(adapter);
  
          return rootView;
      }
  }
  
  public static class SecondFragment extends Fragment {
      private String mId;
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          View rootView = inflater.inflate(R.layout.second_fragment, container, false);
          rootView.findViewById(R.id.imgDummy).setTransitionName(mId);
          return rootView;
      }
  
      public void setId(String id) {
          mId = id;
      }
  }
  }
  </code></pre>
  
  <p>If I change following 2 lines:</p>
  
  <pre><code>secondFragment.setId(imgDummy.getTransitionName());
  trans.addSharedElement(imgDummy, imgDummy.getTransitionName());
  </code></pre>
  
  <p>by</p>
  
  <pre><code>secondFragment.setId(imgDummy2.getTransitionName());
  trans.addSharedElement(imgDummy2, imgDummy2.getTransitionName());
  </code></pre>
  
  <p>then I can see imgDummy2 image transitioning properly, probably since it's outside of recyclerview.</p>
  
  <p>Any idea how can I apply same within recyclerview as well?</p>