我正在尝试在2个片段Blank Blank和BlankFragment2之间使用共享元素动画。 BlankFragment具有回收站视图,而BlankFragment2是详细信息屏幕。他们共享一张图片,而我正在使用新的导航组件。
在BlankFragment中,我正在构建FragmentNavigator.Extras
,并使用共享映像的过渡名称(将其作为回收站视图,并且它们必须是唯一的)将附加项传递给我对navigate
的调用,< / p>
在BlankFragment2中,我收到此名称,将其设置为我的图像并设置setSharedElementEnterTransition
结果是enter动画效果很好,但退出/返回效果却不好,我尝试设置它们而不设置它们(因为我相信导航组件应该为我自己处理),有人可以帮忙吗?
MainActivity导航设置
private void setNavigation() {
navController = Navigation.findNavController(this, R.id.main_fragment);
NavigationUI.setupActionBarWithNavController(this, navController);
}
后退按钮
@Override
public boolean onSupportNavigateUp() {
return Navigation.findNavController(this, R.id.main_fragment).navigateUp()
|| super.onSupportNavigateUp();
}
BlankFragment OnClick
@Override
public void onClick(View view, int position) {
NavController navController = Navigation.findNavController(recyclerView);
FragmentNavigator.Extras extras = new FragmentNavigator.Extras.Builder().addSharedElement(view, view.getTransitionName()).build();
BlankFragmentDirections.ActionBlankFragmentToBlankFragment2 directions = BlankFragmentDirections.actionBlankFragmentToBlankFragment2(view.getTransitionName());
navController.navigate(directions,extras);
}
BlankFragment2 onCreate具有返回/退出转换
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setSharedElementReturnTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.move));
setExitTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.no_transition));
setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.move));
getFragmentArguments();
}
获取参数方法
private void getFragmentArguments(){
if (getArguments() != null){
transitionName = BlankFragment2Args.fromBundle(getArguments()).getTransitionName();
Log.d(TAG, "transition name " + transitionName);
}
}
设置图像过渡名称
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
image = view.findViewById(R.id.image);
image.setTransitionName(transitionName);
text = view.findViewById(R.id.text);
}
答案 0 :(得分:2)
Java
要在返回过渡时解决问题,请使用 viewTreeObserver.addOnPreDrawListener
在BlankFragment中(具有回收视图的片段)
我们需要致电postponeEnterTransition();
,以便过渡
将成为波司蛋白
如下所示在recycleView上设置addOnPreDrawListener
RecyclerView recyclerView = view.findViewById(R.id.recycler_view);
//setup for recycle view adapter
ViewTreeObserver viewTreeObserver = recyclerView.getViewTreeObserver();
viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
@Override
public boolean onPreDraw() {
startPostponedEnterTransition();
return true;
}
});
就是这样。
点击此链接以获取有关ViewTreeObserver的更多信息,请遵循此link
FIN !!