更换一个片段,活动组内的另一个片段 [英] Replacing a fragment with another fragment inside activity group

查看:115
本文介绍了更换一个片段,活动组内的另一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一组活动的内部片段,我想将它与另一片段替换:

I have a fragment inside a group activity and I want to replace it with another fragment:

FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
SectionDescriptionFragment bdf = new SectionDescriptionFragment();
ft.replace(R.id.book_description_fragment, bdf);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();

它正常工作时,这样做是因为没有使用活动组,一个单独的项目,每一件事情能正常工作日志猫作为控制走了进去getview(),但没有视图可见,甚至没有任何异常的话,我想要的书细节片段由部分细节片段所取代。

It works fine when it is done as a seperate project without using activity group, every thing works fine in log cat as control goes inside getview(), but no view is visible, not even any exception arises, I want the book detail fragment to be replaced by section detail fragment.

图书详细信息片段的XML有ID book_description_fragment和XML的部分描述片段有ID section_description_fragment。

Xml of book detail fragment has id book_description_fragment and xml for section description fragment has id section_description_fragment.

以上code是在一个项目的onClick方法,我想,当用户点击水平滚动视图中的项目,则该片段的变化。

The above code is in onClick method of an item, I want that when user taps on an item in horizontal scroll view, then the fragment changes.

推荐答案

片段是很难codeD在XML中,无法替代的。如果你需要替换为另一个片段,你应该动态地添加他们,首先。

Fragments that are hard coded in XML, cannot be replaced. If you need to replace a fragment with another, you should have added them dynamically, first of all.

// 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();

这篇关于更换一个片段,活动组内的另一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆