Recycler to Viewpager共享元素转换:只有在第一次点击时才会出现闪存

时间:2018-05-26 13:06:10

标签: android android-fragments android-viewpager android-transitions shared-element-transition

我正在使用recyclerview将viewpager片段转换为片段共享元素转换我的问题是flash只在viewpager第一次启动时才发生如果我点击相同的item或viewpager flash中的任何其他项目没有发生它完美地工作,可能是什么原因?这是我的代码:

on click of recyclerview item:

holder.iv.setTransitionName(MainActivity.position + "ZoomImage");

            ImagePagerFragment productDetailFragment = new ImagePagerFragment();

                getActive.getSupportFragmentManager()
                        .beginTransaction()
                        .setReorderingAllowed(true)
                        .addSharedElement(holder.iv, holder.iv.getTransitionName())
                        .replace(R.id.fragment, productDetailFragment)
                        .addToBackStack(null)
                        .commit();

在具有viewpager:

的ImagePagerFragment中
 prepareSharedElementTransition();
postponeEnterTransition();

prepareSharedElementTransition():

 Transition transition =
    TransitionInflater.from(getContext())
        .inflateTransition(R.transition.image_shared_element_transition);
setSharedElementEnterTransition(transition);
setEnterSharedElementCallback(
    new SharedElementCallback() {
      @Override
      public void onMapSharedElements(List<String> names, Map<String, View> sharedElements) {
        Fragment currentFragment = (Fragment) viewPager.getAdapter()
            .instantiateItem(viewPager, viewPager.getCurrentItem());
        View view = currentFragment.getView();
        if (view == null) {
          return;
        }
        sharedElements.put(names.get(0), view.findViewById(R.id.image1));
      }
    });

ImagePagerAdapter是FragmentStatePagerAdapter返回ImageFragment,在ImageFragment中:

 Picasso.with(getActivity())
          .load(url)
          .into(ivZoom, new Callback() {
              @Override
              public void onSuccess() {

                  ivZoom.setTransitionName(MainActivity.position + "ZoomImage");
                  getParentFragment().startPostponedEnterTransition();

              }

              @Override
              public void onError() {
                  ivZoom.setTransitionName(MainActivity.position + "ZoomImage");
                  getParentFragment().startPostponedEnterTransition();
              }
          });

0 个答案:

没有答案