我Shared Element Transitions
在活动之间工作。过渡工作正常,但我想知道共享元素过渡何时结束,以便我可以显示其他内容。
我尝试在要过渡到的活动中使用onSharedElementEnd
in SharedElementCallback
,但在过渡开始之前会调用它。
我可以听另一个回调吗?
我Shared Element Transitions
在活动之间工作。过渡工作正常,但我想知道共享元素过渡何时结束,以便我可以显示其他内容。
我尝试在要过渡到的活动中使用onSharedElementEnd
in SharedElementCallback
,但在过渡开始之前会调用它。
我可以听另一个回调吗?
您是否尝试将动画侦听器绑定到内部的共享元素视图onMapSharedElements
?ViewCompat.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);
}
});
请尝试onEnterAnimationComplete()
回调您的活动。
我敢打赌这正是你要找的。
@Override
public void onEnterAnimationComplete() {
super.onEnterAnimationComplete();
//your code
}
这是我在片段中所做的:
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) {
}
});
我在 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()
}
}))
}
所以我可以在开始新动画之前等待共享过渡完成
尝试在 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) {
}
})