我正在实现一个画廊应用,其中有一个Fragment持有一个包含图片的RecyclerView,在点击一张图片后跳转到ViewPager来循环浏览图片。
目前,我正试图仅实现类似这个视频中所示的入场动画。问题在于动画根本不起作用,我显然漏掉了些东西(下面是与转换相关的关键代码):
ViewPager:
public class ViewPagerFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_viewpager, container, false);
Transition transition = TransitionInflater.from(getContext()).inflateTransition(R.transition.fragment_transition);
setSharedElementEnterTransition(transition);
postponeEnterTransition();
return view;
}
GridAdapter:
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.setPhotoImage(new File(mArrayOfPhotos.get(position).photo));
ViewCompat.setTransitionName(holder.photoImage, mPhotoObjects.get(position).photo);
}
@Override
public void onClick(View view) {
// pass an image view that is being clicked...
// ...via listener to MainActivity from where ViewPagerFragment is started
mListener.onImageSelected(photoImage, getAdapterPosition());
}
}
在MainActivity中,我在onClick中实例化ViewPagerFragment:
@Override
public void onImageSelected(View view, int clickedImagePosition) {
ViewPagerFragment fragment = new ViewPagerFragment();
Bundle bundle = new Bundle();
bundle.putInt(ViewPagerFragment.KEY_IMAGE_INDEX, clickedImagePosition);
fragment.setArguments(bundle);
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
// view is an image view that was clicked
fragmentTransaction.addSharedElement(view, ViewCompat.getTransitionName(view));
fragmentTransaction.setReorderingAllowed(true);
fragmentTransaction.replace(R.id.fragment_placeholder, fragment, VIEWPAGER_FRAGMENT);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
最后,在ImageFragment(ViewPager中的单个图像)中我有:
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
mFullImage.setTransitionName(transitionName);
Glide.with(getActivity())
.load(myImage)
.apply(new RequestOptions().diskCacheStrategy(DiskCacheStrategy.ALL))
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
getParentFragment().startPostponedEnterTransition();
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
getParentFragment().startPostponedEnterTransition();
return false;
}
})
.into(mFullImage);
return view;
}
转换名称不是问题,它是唯一的,并且在所有片段之间正常传递。我遵循了这篇文章并阅读了许多其他文章,但感觉我的代码中缺少了一小部分。
编辑:我认为我已经找到了问题所在。过渡效果会随机地工作几次,显然,这是因为当我进行转换时图像尚未准备好。我尝试在MainActivity onCreate中调用
postponeEnterTransition()
,但仍然无法正常工作。图像通过Bundle传递到ImageFragment。仍在寻找解决方案。编辑:
我相信我已经找到了问题所在,我有一个片段,在其中包含一个
TabLayout
,它有2个片段,一个显示所有照片,一个显示标记为收藏的照片。两者都使用相同的RecyclerView
。只有当照片同时位于一个部分时,共享元素转换才起作用。一旦我将照片标记为喜欢,它就会出现在所有照片以及喜欢的照片中,转换就不再起作用。这可能是因为转换不再是唯一的。是否有解决此问题的方法?考虑到两个片段都使用相同的RecyclerView
和相同的适配器类。