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

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

问题描述

我在组活动中有一个片段,我想用另一个片段替换它:

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() 时,在 log cat 中一切正常,但没有视图可见,甚至没有出现任何异常,我想要这本书详细信息片段将被部分详细信息片段替换.

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.

上面的代码是在一个项目的 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.

推荐答案

在 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.

注意:R.id.fragment_container 是您在要将片段带到的活动中选择的布局或容器.

Note: R.id.fragment_container is a layout or container of your choice in the activity you are bringing the fragment to.

// Create new fragment and transaction
Fragment newFragment = new ExampleFragment();
FragmentTransaction transaction = getSupportFragmentManager().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天全站免登陆