Android:共享元素 - 返回转换不起作用

时间:2018-05-08 03:26:51

标签: android android-animation shared-element-transition

我实现了从回收站视图到片段之间的共享元素转换。输入过渡效果很好,但我不知道为什么返回过渡不起作用。

来自第一个片段

的RecyclerView的适配器
public void onBindViewHolder(@NonNull final MenuItemViewHolder holder, int position)
{
  holder.setData(category, data.get(holder.getAdapterPosition()));
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
  {
    MenuItem item = data.get(holder.getAdapterPosition());
    ViewCompat.setTransitionName(holder.name, item.getName());
    ViewCompat.setTransitionName(holder.image, item.getImage());
    ViewCompat.setTransitionName(holder.price, item.getId());
  }

  holder.setViewClick(new View.OnClickListener()
  {
    @Override
    public void onClick(View v)
    {
      try
      {
        if (context != null)
        {
          if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
          {
            showFragment(category, data.get(holder.getAdapterPosition()), holder.name, holder.price, holder.image, holder.isLoaded());
          }
          else
          {
            Gson gson = new Gson();
            Bundle bundle = new Bundle();
            bundle.putString(Values.CAT_DATA, gson.toJson(category));
            bundle.putString(Values.ITM_DATA, gson.toJson(data.get(holder.getAdapterPosition())));
            ((MainActivity) context).showFragment(ProductViewFragment.class, null, bundle);
          }
        }
      }
      catch (NullPointerException | ClassCastException e)
      {
        e.printStackTrace();
      }
    }
  });
}

showFragment();

我尝试在每个片段中添加退出和条目转换,但它仍然无效,所以我将其设置为null,认为动画有冲突,但问题仍然存在。

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void showFragment(MenuCategory category, MenuItem menuItem, TextView name, TextView price,
  ImageView image, boolean isLoaded)
{
  try
  {
    if (context != null)
    {
      if (fragment != null)
      {
        //current fragment
        this.fragment.setSharedElementReturnTransition(
          TransitionInflater.from(context).inflateTransition(android.R.transition.move));
        this.fragment.setExitTransition(null);
      }

      ProductViewFragment fragment = ProductViewFragment.newInstance(category, menuItem, isLoaded);
      //next fragment
      fragment.setEnterTransition(null);
      fragment.setSharedElementEnterTransition(
        TransitionInflater.from(context).inflateTransition(android.R.transition.move));

      FragmentManager manager = ((MainActivity) context).getSupportFragmentManager();
      if (manager != null)
      {
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.setReorderingAllowed(true);
        transaction.addSharedElement(name, ViewCompat.getTransitionName(name));
        transaction.addSharedElement(price, ViewCompat.getTransitionName(price));
        transaction.addSharedElement(image, ViewCompat.getTransitionName(image));
        transaction.replace(R.id.container, fragment);
        transaction.addToBackStack(null);
        transaction.commit();
      }
    }
  }
  catch (NullPointerException | ClassCastException e)
  {
    e.printStackTrace();
  }
}

ProductViewFragment

我在设置视图时有这个

ViewCompat.setTransitionName(name, menuItem.getName());
ViewCompat.setTransitionName(image, menuItem.getImage());
ViewCompat.setTransitionName(price, menuItem.getId());

在我在视图中加载图片后调用supportStartPostponedEnterTransition()

@Override
public void onLoadingComplete(String imageUri, final View view, Bitmap loadedImage)
{
  view.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener()
  {
    @Override
    public boolean onPreDraw()
    {
      if (getActivity() != null)
      {
        view.getViewTreeObserver().removeOnPreDrawListener(this);
        getActivity().supportStartPostponedEnterTransition();
      }
      return true;
    }
  });
}

我似乎不知道问题出在哪里,非常感谢任何帮助谢谢!

2 个答案:

答案 0 :(得分:4)

具有Android导航组件的KOTLIN

对于使用Android导航组件时在这里寻找此问题答案的任何人,您都可以通过将以下行添加到起始片段的onViewCreated函数中来使反向过渡动画起作用:

        postponeEnterTransition()
        view.doOnPreDraw { startPostponedEnterTransition() }

如果要通过单击RecyclerView项打开第二个片段,通常应使用postponeEnterTransition / startPostponedEnterTransition

答案 1 :(得分:0)

我设法通过删除transaction.setReorderingAllowed(true);来解决此问题。