首页 > 解决方案 > SharedElements 过渡在 Go 上有效,但在 Back 上无效

问题描述

我正在学习如何将 sharedelements 动画与导航组件一起使用。

当我调用 new fragment 时,这件事起作用,为对象设置动画,但当我推回时不起作用。

我在回收站视图中的代码

    CardView cardviewBackgroundImage = view.findViewById(R.id.card_main_image);
    TextView storeName = view.findViewById(R.id.store_name);

    Map<View, String> transitionObjects = new HashMap<View, String>();
    transitionObjects.put(cardviewBackgroundImage, cardviewBackgroundImage.getTransitionName());
    transitionObjects.put(storeName, storeName.getTransitionName());

    FragmentNavigator.Extras extras = new FragmentNavigator.Extras.Builder()
            .addSharedElements(transitionObjects)
            .build();

    Bundle bundle = new Bundle();
    bundle.putString("subfix", subfix);
    bundle.putString("key", key);
    Navigation.findNavController(getView()).navigate(R.id.action_mainScreenFragment_to_placeViewFragment, bundle, null, extras);

在目标片段中,我的代码是:

    @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    postponeEnterTransition();
    sharedElementEnterTransition = TransitionInflater.from(getContext()).inflateTransition(android.R.transition.move);
    this.setSharedElementEnterTransition(sharedElementEnterTransition);


}


@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    String subfix = getArguments().getString("subfix");
    String key = getArguments().getString("key");

    CardView cardviewBackgroundImage = view.findViewById(R.id.card_main_image);
    TextView storeName = view.findViewById(R.id.store_name);
    cardviewBackgroundImage.setTransitionName("cardimage" + subfix + key);
    storeName.setTransitionName("storename" + subfix + key);
    startPostponedEnterTransition();
}

提前致谢!

标签: androidtransitionshared-element-transition

解决方案


推荐阅读