试试这个 -
使用不同的过渡名称设置每一行图像 -
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.list_item, null);
}
TextView textView = (TextView) view.findViewById(R.id.textView);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
textView.setTransitionName("transtext" + position);
imageView.setTransitionName("transition" + position);
}
return view;
}
}
然后在项目上单击例如 -
ImageView imageView = (ImageView) view.findViewById(R.id.imageView);
TextView textView = (TextView) view.findViewById(R.id.smallerImageView);
EndFragment endFragment = new EndFragment();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
imageTransitionName = imageView.getTransitionName();
textTransitionName = textView.getTransitionName();
setSharedElementReturnTransition(TransitionInflater.from(
getActivity()).inflateTransition(R.transition.change_image_trans));
setExitTransition(TransitionInflater.from(
getActivity()).inflateTransition(android.R.transition.fade));
endFragment.setSharedElementEnterTransition(TransitionInflater.from(
getActivity()).inflateTransition(R.transition.change_image_trans));
endFragment.setEnterTransition(TransitionInflater.from(
getActivity()).inflateTransition(android.R.transition.fade));
}
Bundle bundle = new Bundle();
FragmentManager fragmentManager = getFragmentManager();
bundle.putString("TRANS_NAME", imageTransitionName);
bundle.putString("TRANS_TEXT", textTransitionName);
endFragment.setArguments(bundle);
fragmentManager.beginTransaction()
.replace(R.id.container, endFragment)
.addToBackStack("Payment")
.addSharedElement(imageView, imageTransitionName)
.addSharedElement(textView, textTransitionName)
.addSharedElement(staticImage, getString(R.string.fragment_image_trans))
.commit();
在 oncreate 方法的 EndFragment 中获取转换名称的键并在视图上设置 -
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle = getArguments();
String actionTitle = "";
Bitmap imageBitmap = null;
String transText = "";
String transitionName = "";
if (bundle != null) {
transitionName = bundle.getString("TRANS_NAME");
actionTitle = bundle.getString("ACTION");
imageBitmap = bundle.getParcelable("IMAGE");
transText = bundle.getString("TRANS_TEXT");
}
getActivity().setTitle(actionTitle);
View view = inflater.inflate(R.layout.fragment_end, container, false);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.findViewById(R.id.listImage).setTransitionName(transitionName);
view.findViewById(R.id.smallerImageView).setTransitionName(transText);
}
((ImageView) view.findViewById(R.id.listImage)).setImageBitmap(imageBitmap);
((TextView) view.findViewById(R.id.smallerImageView)).setText(actionTitle);
return view;
}
相关https://stackoverflow.com/a/31982516/3150663
如果您有解决方案,请投票给我的答案。:)