How to use shared element transitions in Navigation Controller

前端 未结 9 798
醉话见心
醉话见心 2021-01-31 08:06

I would like to add a shared elements transition using the navigation architecture components, when navigating to an other fragment. But I have no idea how. Also in the document

9条回答
  •  忘掉有多难
    2021-01-31 08:48

    For Java

    To make shared element create a method like :

    void sharedNavigation(int id, View... views) {
            FragmentNavigator.Extras.Builder extras = new FragmentNavigator.Extras.Builder();
            for (View view : views)
                extras.addSharedElement(view, view.getTransitionName());
            FragmentNavigator.Extras build = extras.build();
            Navigation.findNavController(getView()).navigate(id,
                    null,
                    null,
                    build);
        }
    

    At the destination class or base class you have to add below code in your onCreate().

    @Override
        public void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setSharedElementEnterTransition(TransitionInflater.from(getContext())
                    .inflateTransition(android.R.transition.move));
        }
    

    And to make transition animation give the id and views to the sharedNavigation() like below :

    sharedNavigation(R.id.action_splashFragment_to_loginFragment,
                            getView().findViewById(R.id.logo));
    

提交回复
热议问题