FirstFragment(包括RecyclerView)和SecondFragment之间如何使用共享元素过渡

How to use shared element transition between FirstFragment (including RecyclerView) and SecondFragment

我可以在没有 RECYCLERVIEW 的情况下实现两个片段之间的共享元素转换!!!

这是第一个片段:

 public class FirstFragment extends Fragment {

ImageView img_small;
LinearLayout layout_ofc_cities;
LinearLayout layout;

public FirstFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_first, container, false);

    img_small = (ImageView) view.findViewById(R.id.img_small);
    layout_ofc_cities = (LinearLayout) view.findViewById(R.id.layout_ofc_cities);
    layout = (LinearLayout) view.findViewById(R.id.layout);

    layout.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

                setSharedElementReturnTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_trans));
                setExitTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.fade));

                SecondFragment secondFragment = new SecondFragment();
                secondFragment.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(R.transition.change_image_trans));
                secondFragment.setEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.fade));

                FragmentTransaction ft = getFragmentManager().beginTransaction()
                        .replace(R.id.container, secondFragment)
                        .addToBackStack(null)
                        .addSharedElement(layout_ofc_cities, "transitionTxt")
                        .addSharedElement(img_small, "transitionImg");
                ft.commit();
            }

        }
    });

    return view;
  }
}

这是第二个片段:

 public class SecondFragment extends Fragment {

public SecondFragment() {

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_second, container, false);

    return view;
  }

}

而且有效! 但是当我在 FirstFragment 中实现 RecyclerView 以将 SharedElementTransition 用于 LinearLayout 并将 RecyclerViewImageView 用于 LinearLayoutImageView 在 SecondFragment 中它不起作用。

我想要一个包含两个 Fragment 的示例代码,第一个 Fragment 中有 RecyclerView,在它们的共享元素之间有动画。谁能帮帮我?

我找到了答案:

解决方案在此页面上: http://jemsdevmobile.com/2016/02/11/lollipop-transition-between-activities/

只是您需要更改 xml 标签。例如将 "imageview" 更改为 "ImageView"