我想使用Android中的TransitionManager和Scene框架创建一个简单的场景转换。从scene1转换到scene2时,转换工作正常,但在相反方向上不起作用。
这个想法是点击按钮(scene1)打开一个带有几个按钮(scene2)的简单布局,然后在用户按下scene2中的按钮后返回到初始状态。
Scene1 - > Scene2工作正常。一切都很好(见上图)。然后我想切换Scene2 - > Scene1并收到以下错误:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:3880)
at android.view.ViewGroup.addView(ViewGroup.java:3733)
at android.view.ViewGroup.addView(ViewGroup.java:3678)
at android.view.ViewGroup.addView(ViewGroup.java:3654)
at android.transition.Scene.enter(Scene.java:177)
at android.transition.TransitionManager.changeScene(TransitionManager.java:199)
at android.transition.TransitionManager.go(TransitionManager.java:365)
at com.test.MainActivity$2.onClick(MainActivity.java:84)
问题: 如何反转Scene2的转换 - >场景1?我在这里缺少什么?
以下是活动和布局的代码:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initToggle();
}
private void initToggle() {
final RelativeLayout sceneBase = (RelativeLayout) findViewById(R.id.toggle);
final ViewGroup scene2closed = (ViewGroup)getLayoutInflater().inflate(R.layout.closed, sceneBase, true);
final ViewGroup scene2opened = (ViewGroup)getLayoutInflater().inflate(R.layout.opened, sceneBase, false);
final Scene sceneOpen = new Scene(sceneBase, scene2opened);
final Scene sceneClose = new Scene(scene2opened, scene2closed);
final Transition t = new AutoTransition();
t.setDuration(100);
scene2closed.findViewById(R.id.toggle_scene).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TransitionManager.go(sceneOpen, t);
}
});
scene2opened.findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TransitionManager.go(sceneClose, t);
}
});
}
}
activity_main.xml中
<RelativeLayout
android:id="@+id/toggle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="8dip"
android:layout_marginRight="8dip"
android:layout_marginLeft="8dip"
android:background="#ffffff">
</RelativeLayout>
closed.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toggle_scene"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:elevation="4dip"
android:padding="5dip"
android:visibility="visible">
<ImageView
android:layout_width="30dip"
android:layout_height="30dip"
android:scaleType="fitCenter"
android:background="#444444" />
</RelativeLayout>
open.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:padding="5dip">
<LinearLayout
android:id="@+id/toggleMapTypes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dip"
android:background="#03A9F4"
android:padding="5dip"
android:text="button1"
android:textAllCaps="false"
android:textColor="#ffffff" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dip"
android:padding="5dip"
android:text="button2"
android:textAllCaps="false"
android:textColor="#03A9F4" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:minHeight="0dip"
android:padding="5dip"
android:text="button3"
android:textAllCaps="false"
android:textColor="#03A9F4" />
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:minHeight="0dip"
android:maxLines="1"
android:padding="5dip"
android:text="button4"
android:textAllCaps="false"
android:textColor="#03A9F4" />
</LinearLayout>
答案 0 :(得分:1)
我设法找出问题的根源,以防任何其他绊倒在同一个问题上。在为代码内的场景充气时,我首先使用LayoutInflater:
将其分配给布局// WRONG 3. parameter is true (add to root layout)
final ViewGroup scene2closed = (ViewGroup)getLayoutInflater().inflate(R.layout.closed, sceneBase, true);
解决方案不是将第一个场景布局添加到根目录,而是启用场景:
// CORRECT 3. param inside .inflate call (do NOT add to root layout)
final ViewGroup scene2closed = (ViewGroup)mInflater.inflate(R.layout.include_map_select_closed, sceneBase, false);
final Scene sceneClose = new Scene(sceneBase, scene2closed);
sceneClose.enter();
使用sceneClose.enter()启动第一个场景后,布局会根据需要切换。