我试图理解Lollipop过渡和TransitionListener,所以我创建了2个简单的活动。活动A和活动B.活动A开始活动B.
活动A在页面底部有一个小绿框。 活动B在页面顶部有一个绿色大框
移动转换按预期顺利进行。
在Activity B中,我为SharedElementEnterTransition和SharedElementReturnTransition添加了TransitionListener。
我的期望是,当进入Acitivity B时,onTransitionStart和onTransitionEnd将为“enter listener”执行,当从Activity B返回时,onTransitionStart和onTransitionEnd将执行“return listener”。但事实并非如此,两个听众都会在进入和返回时被执行。
任何有关它为何如此工作的帮助都表示赞赏。
我添加了一些系统打印,输入活动B时打印了这些内容:
12:59:22.686 #¤ ActivityB.onTransitionStart - Enter
12:59:22.686 #¤ ActivityB.onTransitionStart - Return
12:59:23.000 #¤ ActivityB.onTransitionEnd - Enter
12:59:23.000 #¤ ActivityB.onTransitionEnd - Return
从活动B返回时打印:
13:01:27.584 #¤ ActivityB.onTransitionStart - Enter
13:01:27.584 #¤ ActivityB.onTransitionStart - Return
13:01:27.894 #¤ ActivityB.onTransitionEnd - Enter
13:01:27.894 #¤ ActivityB.onTransitionEnd - Return
我的期望是,在进入活动A时只打印“输入”,从活动B返回时只打印“返回”。
这是我的代码:
活动A:
public class ActivityA extends Activity {
private Activity mActivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_a);
mActivity = this;
final View greenBox = findViewById(R.id.green_box_bottom);
greenBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(mActivity, ActivityB.class);
ActivityOptions options = ActivityOptions
.makeSceneTransitionAnimation(mActivity, greenBox, "move-green-box");
startActivity(intent, options.toBundle());
}
});
}
}
活动B:
public class ActivityB extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_b);
getWindow().getSharedElementEnterTransition().addListener(new Transition.TransitionListener() {
@Override
public void onTransitionStart(Transition transition) {
System.out.println("#¤ ActivityB.onTransitionStart - Enter");
}
@Override
public void onTransitionEnd(Transition transition) {
System.out.println("#¤ ActivityB.onTransitionEnd - Enter");
}
@Override
public void onTransitionCancel(Transition transition) {
}
@Override
public void onTransitionPause(Transition transition) {
}
@Override
public void onTransitionResume(Transition transition) {
}
});
getWindow().getSharedElementReturnTransition().addListener(new Transition.TransitionListener() {
@Override
public void onTransitionStart(Transition transition) {
System.out.println("#¤ ActivityB.onTransitionStart - Return");
}
@Override
public void onTransitionEnd(Transition transition) {
System.out.println("#¤ ActivityB.onTransitionEnd - Return");
}
@Override
public void onTransitionCancel(Transition transition) {
}
@Override
public void onTransitionPause(Transition transition) {
}
@Override
public void onTransitionResume(Transition transition) {
}
});
}
布局活动A:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".ActivityA">
<View
android:id="@+id/green_box_bottom"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="@android:color/holo_green_light"
android:layout_alignParentBottom="true"
android:transitionName="move-green-box"/>
</RelativeLayout>
布局活动B:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".ActivityB">
<View
android:id="@+id/green_box_bottom"
android:layout_width="300dp"
android:layout_height="300dp"
android:background="@android:color/holo_green_light"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:transitionName="move-green-box"/>
</RelativeLayout>
答案 0 :(得分:1)
如果未设置ReturnTransition,则Enter转换将用作ReturnTransition。所以你基本上是在同一个转换上设置了两个监听器。尝试为ReturnTransition设置不同的转换,看看是否无法解决问题。