为片段添加事务转换

Add transaction transition for fragments

当我使用 ShowViewModel 导航到 activity 时,它的动画效果很好。但是当目标是 Fragment 时它不会。还有办法添加这个吗?

我看到在本机 android 中,您可以将其添加到 FragmentTransaction,但由于 MvvmCross 为我们处理了它,我认为还有另一个地方可以处理它。

处理片段事务的代码是 activity 实现 IMvxFragmentHost 中的 Show 方法,负责处理正在显示的特定片段。为了改变动画,你需要在显示片段时使用SetCustomAnimations方法。

我通常做的是创建一个 BaseFragmentView class 将进入和离开动画公开为属性。显示片段时,我可以像这样简单地使用这些属性:

var transaction = SupportFragmentManager
                   .BeginTransaction()
                   .SetCustomAnimations(fragmentView.EnterAnimation, fragmentView.ExitAnimation)
                   .Replace(targetId, fragmentView)
                   .Commit();

当使用MvxChachingFragmentView时,您可以简单地覆盖OnBeforeFragmentChanging方法并使用第二个参数来添加您想要的自定义动画。

查看MvxCachingFragmentView class and, if you don't know how to use the new Fragments from MvvmCross 4, refer to

可以看到如何实现IMvxFragmentHost接口