我在抽屉式导航工作代替它和现在使用的:。如何获得当前片段,并与另一片段
getSupportFragmentManager()调用BeginTransaction()代替(R.id.mainfragment,F1) 。承诺();
但正如你所看到的那样,总是用新的替代主碎片。谢谢!
我在抽屉式导航工作代替它和现在使用的:。如何获得当前片段,并与另一片段
getSupportFragmentManager()调用BeginTransaction()代替(R.id.mainfragment,F1) 。承诺();
但正如你所看到的那样,总是用新的替代主碎片。谢谢!
您可以执行beginTransaction()。replace(container,newFragment,tag),其中container是xml文件中ViewGroup的标识。所以被替换的东西是当前在容器中的任何碎片。
用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();
所以,你只能通过下面以下链接替换“动态添加的片段”:
final FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(getId(), new SearchVideoDownloadFragment(), "NewFragmentTag");
ft.commit();
此方法替换您在与容器的任何片段id'mainfragment'。如果您想替换另一个,请选择其容器的ID。请记住,除了替换之外,还有其他方法,比如添加,允许您在同一个容器中重叠片段。 – droidpl