如何在FirstFragment(包括RecyclerView)和SecondFragment之间使用共享元素过渡 [英] How to use shared element transition between FirstFragment (including RecyclerView) and SecondFragment

查看:545
本文介绍了如何在FirstFragment(包括RecyclerView)和SecondFragment之间使用共享元素过渡的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I could implement shared element transition between two fragments WITHOUT RECYCLERVIEW!!!

这是FirstFragment:

This is FirstFragment:

 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;
  }
}

这是SecondFragment:

and this is SecondFragment:

 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时,则不起作用.

And it works! but when I implements a RecyclerView in FirstFragment to use SharedElementTransition for a LinearLayout and an ImageView of RecyclerView to a LinearLayout and an ImageView in SecondFragment it doesn't work.

我想要一个带有两个Fragment的示例代码,第一个具有RecyclerView,并且在它们的共享元素之间具有动画效果.谁能帮我吗?

I want a sample code with two Fragments that the first one has RecyclerView in it, with animation between their shared elements. Can anyone help me?

推荐答案

我找到了答案:

解决方案在此页面上: http://jemsdevmobile.com/2016/02/11/lollipop-活动之间的过渡/

the solution is on this page: http://jemsdevmobile.com/2016/02/11/lollipop-transition-between-activities/

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

just you need to change xml tags. for example change "imageview" to "ImageView"

这篇关于如何在FirstFragment(包括RecyclerView)和SecondFragment之间使用共享元素过渡的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆