2015-10-04 77 views
1

我在抽屉式导航工作代替它和现在使用的:。如何获得当前片段,并与另一片段

getSupportFragmentManager()调用BeginTransaction()代替(R.id.mainfragment,F1) 。承诺();

但正如你所看到的那样,总是用新的替代主碎片。谢谢!

+0

此方法替换您在与容器的任何片段id'mainfragment'。如果您想替换另一个,请选择其容器的ID。请记住,除了替换之外,还有其他方法,比如添加,允许您在同一个容器中重叠片段。 – droidpl

回答

1

您可以执行beginTransaction()。replace(container,newFragment,tag),其中container是xml文件中ViewGroup的标识。所以被替换的东西是当前在容器中的任何碎片。

0

用XML硬编码的片段不能替换。如果你需要用另一个替换一个片段,首先应该动态添加它们。

// Create new fragment and transaction 
Fragment newFragment = new ExampleFragment(); 
FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

// Replace whatever is in the fragment_container view with this fragment, 
// and add the transaction to the back stack if needed 
transaction.replace(R.id.fragment_container, newFragment); 
transaction.addToBackStack(null); 

// Commit the transaction 
transaction.commit(); 

所以,你只能通过下面以下链接替换“动态添加的片段”:

http://notionink.wikidot.com/rajeshbabu

http://sapandiwakar.in/replacing-fragments/

+0

通过在xml中硬编码你的意思是有一个XML布局文件为他们?如果是这样,我会如何设计布局风格? – Engima

+0

是的,您无法替换布局文件中静态定义的片段。您只能通过FragmentTransaction替换您动态添加的片段。 – prat

+0

请检查编辑。愿它能帮助你 – prat

0
final FragmentTransaction ft = getFragmentManager().beginTransaction(); 
ft.replace(getId(), new SearchVideoDownloadFragment(), "NewFragmentTag"); 
ft.commit();