问题描述
我正在尝试这一天,但是我不知道出了什么问题!我有一个包含两个片段的活动,在第一个片段中,我有一个RecyclerView包含一个imageview,在另一个片段中,我有另一个imageview.
I'm trying to to this for a day but i don't know what is the problem! I have an activity with two fragments which in the first one i have a RecyclerView contains an imageview and on the other fragment i have another imageview.
自定义视图层imageview:
custom view layer imageview:
<ImageView
android:id="@+id/clst_img_cover"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop"
app:srcCompat="@drawable/no_image"
android:transitionName="@string/transition_name_cover"/>
片段两个imageview:
fragment two imageview:
<ImageView
android:id="@+id/img_movie_cover"
android:transitionName="@string/transition_name_cover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="@color/colorWhite" />
,并在我的适配器中的列表视图中:
and in my adapter for the list view:
MovieDetailsFragment movieDetails = new
MovieDetailsFragment().newInstance(String.valueOf(model.getId()));
FragmentManager manager = ((MainActivity)v.getContext()).getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
String a = v.getContext().getResources().getString(R.string.transition_name_cover);
cover.setTransitionName(a);
movieDetails = new MovieDetailsFragment().newInstance(String.valueOf(model.getId()),((BitmapDrawable)cover.getDrawable()).getBitmap());
Transition changeTransform = TransitionInflater.from(context).inflateTransition(R.transition.default_transition);
changeTransform.setDuration(1500);
MovieList current = (MovieList) manager.findFragmentById(R.id.activity_main_container_body);
movieDetails.setSharedElementReturnTransition(new CustomTransition());
movieDetails.setSharedElementEnterTransition(new CustomTransition());
movieDetails.setEnterTransition(new Fade());
current.setExitTransition(new Fade());
String x = cover.getTransitionName();
transaction.addSharedElement(cover,a);
}
else{
transaction.setCustomAnimations(R.animator.fr_fade_in,R.animator.fr_slide_out_right,R.animator.fr_slide_in_right,R.animator.fr_fade_out);
}
transaction.replace(R.id.activity_main_container_body,movieDetails,"movieDetails");
transaction.addToBackStack(null);
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
第二个片段有两个newInstance方法,如下所示:
and my second fragment has two newInstance method like this:
public static MovieDetailsFragment newInstance(String Movie_ID) {
MovieDetailsFragment fragment = new MovieDetailsFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, Movie_ID);
fragment.setArguments(args);
return fragment;
}
public static MovieDetailsFragment newInstance(String Movie_ID,Bitmap bmp) {
MovieDetailsFragment fragment = new MovieDetailsFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, Movie_ID);
args.putParcelable(ARG_PARAM2, bmp);
fragment.setArguments(args);
return fragment;
}
这是CustomTransition.java:
and this is CustomTransition.java :
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public class CustomTransition extends TransitionSet {
public CustomTransition(){
setOrdering(ORDERING_TOGETHER);
addTransition(new ChangeBounds()).addTransition(new ChangeTransform()).addTransition(new ChangeImageTransform());
}
}
它可以起到淡入淡出的作用,但不适用于图像!
it acts for the fade but doesn't work for the image!
更新我建议在我的git存储库中使用此示例,对于初学者来说,这是一个很好的示例.
UpdateI suggest to use this example in my git repository, its a great example for beginners.
推荐答案
最后,一个星期后,我罚款了我的上锁错误.
Finally, i fined my ****ing mistake after a week.
最好使用android.support.v4.app.Fragment
,这就是为什么它不起作用的原因
It's Best To Use android.support.v4.app.Fragment
thats why it wasn't work
i在android.support.v4.app.Fragment
和android.app.Fragment
i highly recommend you to use this tutorial, specially download the git repository
这篇关于共享元素过渡Android无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!