我有一个简单的布局,其中包含两个片段。两个片段中的一个会周期性地取代它,另一个片段在我使用它的整个时间内将保持一致。出于某种原因,删除/添加它工作得很好,但替换它会导致其他片段消失。我从调试代码知道,片段只是被分离,没有明显的原因。为什么会这样呢?如果我使用后者,我必须非常小心地确保QuestionArea先走了......消失片段(Dettached without request)
这些代码行来自FragmentActivity类。
fragManager.beginTransaction()
.replace(R.id.QuestionArea, getQuestionPostView(), "QuestionArea")
.commit();
fragManager.beginTransaction()
.remove(fragManager.findFragmentById(R.id.QuestionArea))
.add(R.id.QuestionArea, getQuestionPostView(), "QuestionArea")
.commit();
这是我希望的是其他相关代码:
主显示器的XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical" >
<com.pearsonartphoto.FontFitTextView
android:id="@+id/Name"
style="@style/bigText"
android:background="@color/blue"
android:gravity="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"/>
<TextView android:id="@+id/Instructions"
style="@style/medText"
android:layout_width="fill_parent"
android:layout_below="@id/PowerName"
/>
<fragment
android:name="com.pearsonartphoto.NumberDisplay"
android:gravity="center"
android:id="@+id/QuestionArea"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/Instructions"/>
<fragment
android:name="com.pearsonartphoto.AbstractAnswerFragment"
android:id="@+id/AnswerArea"
style="@style/bigText"
android:gravity="center"
android:layout_below="@+id/QuestionArea"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="530sp"/>
</RelativeLayout>
FragmentActivity的一部分,该树立AnswerArea
第一次(来自FragmentActivity)设置了QuestionArea。
fragManager.beginTransaction()
.replace(R.id.QuestionArea, getQuestionPreView(), "QuestionArea")
.commit();
功能getPostView()和getPreView()
@Override
Fragment getQuestionPreView() {
NumberDisplay display=(NumberDisplay)manager.findFragmentById(R.id.QuestionArea);
display.setNumber(-1);
return display;
}
@Override
Fragment getQuestionPostView() {
NumberDisplay display=(NumberDisplay)manager.findFragmentById(R.id.QuestionArea);
display.setNumber(answer);
return display;
}
这提供了我需要算出这个信息的关键位。谢谢!我打算为未来的用户发布更详细的版本,但是,如果没有您的帮助,我不能拿出这个版本。 – PearsonArtPhoto
我刚刚给你一个赞美。这一举克服了我一直面临的很多问题。非常感谢! – PearsonArtPhoto