![](/img/trans.png)
[英]cannot resolve FragmentTransaction.add() method . Why am I getting this error?
[英]Fragments overlapping when I am using fragmentTransaction.add method
我遇到了一些奇怪的片段問題,我在這兩個片段上顯示谷歌 map,同時從Home Fragment
切換到Request cab
片段,在Request cab
片段中,片段重疊,我可以在背景中看到Home Fragment
並且它也可點擊我也可以移動Home Fragment
的 map。 當我使用fragmentTransaction.add(R.id.frame, fragment, fragmenttag);
時會出現此問題但是如果我使用替換,那么它的工作正常,但我想恢復視圖onBackPressed
,這是通過替換碎片網絡是不可能的。 我附上截圖和代碼。
public void changeFragment(final Fragment fragment, final String fragmenttag) {
try {
drawer_close();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction =
fragmentManager.beginTransaction().addToBackStack(null);
fragmentTransaction.add(R.id.frame, fragment, fragmenttag);
fragmentTransaction.commit();
} catch (Exception e) {
}
}
您只需要在添加片段時添加背景。
add(): adds the new fragment on the top of another fragment that's why you can see below fragment
replace(): removes everything then adds the new fragment
add 和 replace 的工作方式完全不同,而 replace 從膨脹的視圖組中刪除當前片段。 add 方法只是在 ViewGroup 中添加新的布局。 這就是為什么你可以看到你的片段,這就是為什么它的可點擊只是將它們視為普通視圖而不是片段。 現在您可以將白色背景添加到第二個片段的根布局並使其可點擊它應該可以解決您的所有問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.