一个片段正在绘制另一个片段
问题描述:
我有2个片段,一个用于游戏,另一个用于得分。 当我从游戏片段移动到得分片段时,效果很好,但是当我试图移回游戏时,它会在同一个容器中绘制两个片段(即使使用remove函数)。一个片段正在绘制另一个片段
public void nextLvl(Fragment current, Fragment next, String score, String nextTag, int LvlTime){
Bundle args = new Bundle();
args.putString("scoreKey", score);
args.putInt("levelTime", LvlTime);
next.setArguments(args);
FragmentManager fm = getFragmentManager();
if (fm != null){
FragmentTransaction ft = fm.beginTransaction();
ft.remove(current);
ft.add(R.id.fragment_place, next, nextTag);
ft.commit();
Log.d("FM","MOVED TO "+next.toString());
}else{
Log.d("FM","fm is null");
}
}
这是我用来在片段之间移动的代码。
这里有一个形象展示我的意思在“彼此的顶部正在制定”:
http://i.imgur.com/Ylw8iPV.png
1 =游戏片段。
2 =分数片段。
3 =从乐谱片段移动到游戏片段的结果。
答
尝试使用ft.replace
代替remove()
和add()
例如:
ft.beginTransaction()
.replace(R.id.container, fragment)
.commit();
+0
仍然将其绘制在另一个之上:\ – cnsdH 2015-02-08 10:11:04
答
使用替代片段,而不是添加
例如:
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_place, next, nextTag);
ft.commit();
我有一个类似的问题一次。我在我的XML文件中使用片段作为属性,并试图添加一个片段。 我在xml中将片段更改为FrameLayout,并且一切正常。 那么你的“R.id.fragment_place”是什么样的属性? – Furedal 2015-02-08 10:01:08
@Furedal这是一个片段。我会将其更改为FrameLayout并再试一次,如果它工作或没有更新,我会更新它。 编辑: 没有工作不幸.. – cnsdH 2015-02-08 10:04:29
如果我们能够帮助你,你应该上传所有相关的代码,以便在切换片段时使用。 – Furedal 2015-02-08 10:56:12