How to start shared element transition using Fragments?

前端 未结 8 1406
心在旅途
心在旅途 2020-12-04 06:32

I am trying to implement transitions between fragments which have \"shared elements\" as described in the new material design specs. The only method I can find is the Activi

8条回答
  •  旧时难觅i
    2020-12-04 07:20

    I searched for SharedElement in fragments and I find very useful source code on GitHub.

    1.first you should define transitionName for your Objects(Like ImageView) in both Fragments layout(We add a button in fragment A for handling click event):

    fragment A:

      
    

    fragment B:

        
    
    1. Then you should write this code in your transition file in transition Directory(if you haven't this Directory so create One: res > new > Android Resource Directory > Resource Type = transition > name = change_image_transform ):

    change_image_transform.xml:

     
    
      
      
      
      
    
    
    1. In the last step you should complete codes in java:

    fragment A:

    public class FragmentA extends Fragment {
    
        public static final String TAG = FragmentA.class.getSimpleName();
    
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            // Inflate the layout for this fragment
            return inflater.inflate(R.layout.fragment_a, container, false);
        }
    
        @Override
        public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
            super.onViewCreated(view, savedInstanceState);
            final ImageView imageView = (ImageView) view.findViewById(R.id.fragment_a_imageView);
            Button button = (Button) view.findViewById(R.id.fragment_a_btn);
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    getFragmentManager()
                            .beginTransaction()
                            .addSharedElement(imageView, ViewCompat.getTransitionName(imageView))
                            .addToBackStack(TAG)
                            .replace(R.id.content, new FragmentB())
                            .commit();
                }
            });
        }
    }
    

    fragment B:

    public class FragmentB extends Fragment {
    
        @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
                setSharedElementEnterTransition(TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move));
    
        }
    
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            return inflater.inflate(R.layout.fragment_b, container, false);
        }
    }
    

    don't forget to show your "A" fragment in your activity:

     @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            getSupportFragmentManager()
                    .beginTransaction()
                    .add(R.id.content, new SimpleFragmentA())
                    .commit();
        }
    

    source : https://github.com/mikescamell/shared-element-transitions

提交回复
热议问题