我在片段之间转换时遇到问题。如以下GIF:https://imgur.com/a/PDogrxV所示,片段A到B之间的过渡非常不稳定。如果您注意到,当我单击fab按钮时,过渡将在屏幕中间停止。我尝试调试它以减慢动画的速度,这是结果(带有横向过渡):https://imgur.com/a/1ipQvfW
我有一个带有FrameLayout的NavigationBar片段,在其中添加了我的recyclerViewFragment(继承自BaseFragment)。我的目标确实是要使片段A(没有配对的设备)到片段B(包含第一个GIF链接所示的包含回收站视图)之间的过渡不平滑,
供参考:
一个示例NavigationController(继承自我的NavBar片段):
public class ExampleController extends NavBarFragment {
public static ExampleController newInstance() {
ExampleController fragment = new ExampleController();
fragment.setArguments(fragment.createInitBundle());
return fragment;
}
@Override
protected BaseFragment rootFragment() {
return RecyclerViewFragment.newInstance();
}
@Override
public Bundle createInitBundle() {
return new Bundle();
}
}
在NavBarFragment中找到的initializeLayout方法中调用rootFrament()方法(在OnCreateView()中调用)-这只是为了提供一些上下文:
@Override
protected void initializeLayout(View view) {
super.initializeLayout(view);
this.toolbar.setNavigationOnClickListener(this::navigationButtonClick);
this.toolbar.setOnMenuItemClickListener(this::navigationMenuClick);
progressBar.getLayoutParams().height = 0;
progressBar.requestLayout();
this.setContentFragment(view, this.rootFragment());
}
和setContentFragment调用事务管理器:
private void setContentFragment(View view, BaseFragment frag) {
Context context = getActivity() != null ? getActivity().getApplicationContext() : null;
if (frag == null || view == null || context == null) {
return;
}
this.currentFragment = frag;
configureUIComponents(context, frag, 0);
getChildFragmentManager().beginTransaction()
.replace(view.findViewById(R.id.main_container).getId(), frag, createFragmentTag(0))
.commit();
}
RecyclerViewFragment的示例:
public class RecyclerViewFragment extends BaseRecyclerViewFragment {
public static RecyclerViewFragment newInstance(){
//navbar implement
Bundle args = new Bundle();
args.putString(NavigationBarConfig.KEY_NAV_BAR_TITLE, "Paired Devices");
args.putBoolean(NavigationBarConfig.KEY_SHOW_NAV_BAR, true);
args.putBoolean(FloatingActionButtonConfig.KEY_SHOW_FAB_BUTTON, true);
RecyclerViewFragment fragment = new RecyclerViewFragment();
fragment.setArguments(args);
return fragment;
}.
我尝试了一些操作:1)我尝试使用侦听器来等待转换的“结束”以等待加载数据。这是基于以下堆栈溢出问题:Nested fragments transitioning incorrectly。我实现如下(我不会分享整个代码,原因是什么)。然后在我加载模型时使用didDisappear / didAppear()方法:
@Override
public Animator onCreateAnimator(int transit, boolean enter, int nextAnim) {
Animator animator = null;
// Check for "Show" Transition ids
if (showTransitionAnimationIds.contains(nextAnim)) {
animator = AnimatorInflater.loadAnimator(getActivity(), nextAnim);
if (animator != null && enter) {
animator.addListener(new TransitionAnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
if (previousFragment != null) {
previousFragment.willDisappear();
}
willAppear();
}
@Override
public void onAnimationEnd(Animator animation) {
transitionDone.set(true);
if (previousFragment != null) {
previousFragment.didDisappear();
}
didAppear();
}
});
}
} else if (hideTransitionAnimationIds.contains(nextAnim)) {
animator = AnimatorInflater.loadAnimator(getActivity(), nextAnim);
if (animator != null && !enter) {
animator.addListener(new TransitionAnimatorListener() {
@Override
public void onAnimationStart(Animator animation) {
if (previousFragment != null) {
previousFragment.willAppear();
}
willDisappear();
}
@Override
public void onAnimationEnd(Animator animation) {
if (previousFragment != null) {
previousFragment.didAppear();
}
didDisappear();
}
});
}
}
我将这种方法用于模型加载:
protected void waitForTransition() {
if (transitionDone != null) {
try {
transitionDone.get();
} catch (Exception e) {
e.printStackTrace();
}
}
}
通常在加载模型之前可以帮助我“等待”动画结束。我以为这是问题所在。
我的目标实际上是使动画更加流畅。我知道此问题中显示的“提供图像”(换句话说)的技巧:Nested fragments disappear during transition animation,但我正在寻找更清洁的东西。我当时想可能是即时创建两个片段?即:有一个带有2 frameLayout的空布局,其中我可以在运行时注入两个片段。尽管我还没有尝试过,但它似乎不是非常模块化。