Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android shared element transition between fragments having recyclerview & details view

I have 2 fragments: fragment 1 containing recyclerview & 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.

Code is as follows: MyActivity.java

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<ImageAdapter.ViewHolder> {
    public interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    private OnItemClickListener mItemClickListener;

    List<Integer> items = new ArrayList<Integer>();
    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;
    }
}
}

If I change following 2 lines:

secondFragment.setId(imgDummy.getTransitionName());
trans.addSharedElement(imgDummy, imgDummy.getTransitionName());

by

secondFragment.setId(imgDummy2.getTransitionName());
trans.addSharedElement(imgDummy2, imgDummy2.getTransitionName());

then I can see imgDummy2 image transitioning properly, probably since it's outside of recyclerview.

Any idea how can I apply same within recyclerview as well?

like image 805
Ankur Avatar asked Jan 17 '15 13:01

Ankur


1 Answers

I know its way late. But was struggling with this and finally got transition animation from recycler view in fragment A to fragment B working.

try changing imgDummy.getTransitionName()) in the below line

    trans.addSharedElement(imgDummy, imgDummy.getTransitionName());

to the transition name of the ImageView in fragmentB

like image 73
Ragesh Ramesh Avatar answered Nov 09 '22 00:11

Ragesh Ramesh