36

Shared Element Transitions在活动之间工作。过渡工作正常,但我想知道共享元素过渡何时结束,以便我可以显示其他内容。

我尝试在要过渡到的活动中使用onSharedElementEndin SharedElementCallback,但在过渡开始之前会调用它。

我可以听另一个回调吗?

4

5 回答 5

43

您是否尝试将动画侦听器绑定到内部的共享元素视图onMapSharedElementsViewCompat.animate(view)会给你一个新的或缓存ViewPropertyAnimator(Compat)的然后绑定动画监听器应该是微不足道的。不过,我还没有尝试过。

setEnterSharedElementCallback(new SharedElementCallback() {
            @Override
            public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
                super.onMapSharedElements(names, sharedElements);
                View keySharedElementView = sharedElements.get("keySharedElement");
                if(keySharedElementView != null){
                    ViewCompat.animate(keySharedElementView).setListener(new ViewPropertyAnimatorListenerAdapter(){
                        @Override
                        public void onAnimationEnd(View view) {
                            super.onAnimationEnd(view);
                        }
                    });
                }
            }
        });

添加Transition.Listener到共享元素过渡怎么样?

 Transition sharedElementEnterTransition = getWindow().getSharedElementEnterTransition();
 sharedElementEnterTransition.addListener(new TransitionListenerAdapter() {
         @Override
          public void onTransitionEnd(android.support.transition.Transition transition) {
                    super.onTransitionEnd(transition);
           }
      });
于 2015-11-22T20:13:41.243 回答
19

请尝试onEnterAnimationComplete()回调您的活动。

我敢打赌这正是你要找的。

 @Override
    public void onEnterAnimationComplete() {
        super.onEnterAnimationComplete();

    //your code 
    }
于 2015-11-24T13:26:43.533 回答
8

这是我在片段中所做的:

Transition sharedElementEnterTransition = getActivity().getWindow().getSharedElementEnterTransition();
    sharedElementEnterTransition.addListener(new Transition.TransitionListener() {
        @Override
        public void onTransitionStart(Transition transition) {

        }

        @Override
        public void onTransitionEnd(Transition transition) {

        }

        @Override
        public void onTransitionCancel(Transition transition) {

        }

        @Override
        public void onTransitionPause(Transition transition) {

        }

        @Override
        public void onTransitionResume(Transition transition) {

        }
    });
于 2017-09-12T18:47:01.870 回答
0

我在 Kotlin 中使用导航组件努力做到这一点,但设法让它与它一起工作

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    setAndPostponeEnterAnimation()
    _binding = PokemonDetailFragmentBinding.inflate(inflater, container, false)
    setInsets()
    handleNavigationArgs()
    return binding.root
}

private fun setAndPostponeEnterAnimation() {
    postponeEnterTransition()
    sharedElementEnterTransition = TransitionInflater.from(context)
        .inflateTransition(R.transition.shared_element_transition)
    addSharedElementListener()
}

private fun addSharedElementListener() {
    (sharedElementEnterTransition as TransitionSet).addListener((object :
        TransitionListenerAdapter() {
        override fun onTransitionEnd(transition: Transition) {
            super.onTransitionEnd(transition)
            createRevealAnimation()
        }
    }))
}

所以我可以在开始新动画之前等待共享过渡完成

于 2020-09-16T20:02:09.737 回答
0

尝试在 Fragment A 上添加 TransitionListener,而不是 B。并使用android.support.transition.Transition

片段 A

            val anim = DetailsTransition()
            anim.addListener(object : android.support.transition.Transition.TransitionListener
            {
                override fun onTransitionEnd(transition: android.support.transition.Transition) {
                }

                override fun onTransitionResume(transition: android.support.transition.Transition) {
                }

                override fun onTransitionPause(transition: android.support.transition.Transition) {
                }

                override fun onTransitionCancel(transition: android.support.transition.Transition) {
                }

                override fun onTransitionStart(transition: android.support.transition.Transition) {
                }

            })
于 2018-06-10T17:55:40.903 回答