I want to go from a list view to the detail view and therefore, I use following OnClickListener
in my list:
@Override
public void onClick(View view)
{
Bet bet = (Bet)view.getTag();
FragmentManager fm = getActivity().getSupportFragmentManager();
BetDetailFragment f = BetDetailFragment.create(bet);
String tag = f.getClass().getName();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
setSharedElementReturnTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.move));
f.setSharedElementEnterTransition(TransitionInflater.from(getActivity()).inflateTransition(android.R.transition.move));
}
FragmentTransaction ft = fm.beginTransaction()
.replace(R.id.frame_container, f, tag)
.addToBackStack(tag);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
{
L.d(this, "TRANS: " + TransitionUtil.getTransitionNameBetLogo1(bet) + "|" + view.findViewById(R.id.ivLogo1));
L.d(this, "TRANS: " + TransitionUtil.getTransitionNameBetLogo2(bet) + "|" + view.findViewById(R.id.ivLogo2));
ft.addSharedElement(view.findViewById(R.id.ivLogo1), "1");//TransitionUtil.getTransitionNameBetLogo1(bet));
ft.addSharedElement(view.findViewById(R.id.ivLogo2), "2");//TransitionUtil.getTransitionNameBetLogo2(bet));
}
ft.commit();
}
My functions return unique names, I have two different views, but still it does not work.
I already commented unnecessary functions out and wrote some unique transaction names in there by hand... But still, I get this exception, in the line of the first addSharedElement
:
java.lang.IllegalArgumentException: Unique transitionNames are required for all sharedElements
at android.support.v4.app.BackStackRecord.addSharedElement
EDIT
Without the shared elements, everything is working perfectly fine...
答案 0 :(得分:40)
问题是,addSharedElement
没有设置视图的事务名称!
因此,在我的示例中,我必须使用以下代码进行设置:
ViewCompat.setTransitionName(view.findViewById(R.id.ivLogo1), "1");
ViewCompat.setTransitionName(view.findViewById(R.id.ivLogo2), "2");
将此视图添加到FragmentTransaction
...
之后的工作正常并且符合预期:
ft.addSharedElement(view.findViewById(R.id.ivLogo1), "1");
ft.addSharedElement(view.findViewById(R.id.ivLogo2), "2");
答案 1 :(得分:2)
您只需将transitionName
设置为您的共享元素。 无需选择与您的共享元素 名称完全相同的名称(已将其作为addSharedElement()
方法的第二个参数传递)。
此名称(addSharedElement()
的第二个参数)必须等于目标片段中共享元素的transitionName
。参见here。
所以插入就足够了:
ViewCompat.setTransitionName(view.findViewById(R.id.ivLogo1), "AnyThing");
ViewCompat.setTransitionName(view.findViewById(R.id.ivLogo2), "EveryThing");
在调用addSharedElement(...)
之前。
答案 2 :(得分:1)
使用此代码
ViewCompat.setTransitionName(holder.ivImage, "value");
答案 3 :(得分:1)
必须在每个片段的xml布局元素中设置相同的transitionName。例如:
片段A:
<TextView
android:id="@+id/my_text_view"
...
android:transitionName="transtion_name_example"/>
片段B:
<TextView
android:id="@+id/my_text_view"
...
android:transitionName="transtion_name_example"/>
代码将是这样的:
yourTransaction.addSharedElement(view, view.transactionName)
答案 4 :(得分:0)
If your onClickListener
is part of your fragment, not parent Activity
, then you are doing things wrong here. Your fragment should notify parent activity what it wants and Activty should deal with it (i.e. by replacing fragments etc). Fragment should never do this by itself. Also if all you need is to go from detail view to list, then I assume you entered your detail view from that list. If so, all you need is to pop last element (fragment view fragment) fromt back stack. See: https://developer.android.com/reference/android/app/FragmentManager.html