how to Shared element transition from a fragment to an activity

回眸只為那壹抹淺笑 提交于 2019-11-30 02:24:20

问题


I have three fragments inside a ViewPager in an activity, I want to achieve shared element transition from one of the fragments to another activity. The transition is from a recycler view which is inside a fragment which is inside a viewpager which is inside an Activity Activity->ViewPager->Fragment->Recyclerview

I have searched each every places internet but there are info only about shared element transition from one fragment to another and one activity to another. There is no content about transition from fragment to activity

holder.poster.setTransitionName("posterX");
            ActivityOptionsCompat options = ActivityOptionsCompat.makeSceneTransitionAnimation((Activity) con,holder.poster,holder.poster.getTransitionName());
            Log.e("Animation", "Success");
            //startActivity((Activity) con,intent,options.toBundle());
            c.startActivity(intent,options.toBundle());

On using the above code the second activity is launched but nothing is visible, For example the second activity contains a FAB which when clicked youtube is launched. I know where the FAB is so when i click blindly, youtube launched correctly but nothing is visible in second activity

D/ViewRootImpl: changeCanvasOpacity: opaque=true

Its one of the logcats. I think this must be the problem!!


回答1:


I had the same problem, I couldn't find anything that helps sorry, but maybe you should reconsider why would you need a Fragment to Activity relation when you could work with a Fragment to Fragment or Activity to Activity relation.

I've solved my problem that way changing my code to a Fragment to Fragment relation and there is plenty documentation and examples about that Shared Element Transitions




回答2:


I finally found the answer, the startActivity calling method is different. You have to call

startActivityFromFragment(fragment, intent, req_code, options.toBundle());

from the AppCompactActivity




回答3:


Fragment to Activity is the same as Activity to Activity, Because your Fragment is inside an Activity.




回答4:


I think using Pairs as below:

 Pair[] pairs = new Pair[1];
            pairs[0] = new Pair<View, String>(tvArtifacts, "itemTrans");
            ActivityOptions options = null;
            if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
                options = ActivityOptions.makeSceneTransitionAnimation(getActivity(), pairs);
            }

            Intent i = new Intent(getActivity(), ItemDetailActivity.class);
            i.putExtra("item_name", "item 2");
            if (options != null) {
                startActivity(i, options.toBundle());
            } else {
                startActivity(i);
            }

This worked for me! Thanks..



来源:https://stackoverflow.com/questions/38593474/how-to-shared-element-transition-from-a-fragment-to-an-activity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!