Android 以编程方式替换片段
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10122570/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Replace a fragment programmatically
提问by VishalSoni
I have three fragments as shown in below figure. I have added all these three fragments in LinearLayout using .xml file and when my launcher activity starts I load that .xml layout using setContentView.
I have some controls on fragment2. Clicking on any one loads the
fragment4 programmatically using FragmentTransaction and commit method. This fragments is added to the screen but the problem is the prgrammatically added fragment4 take the whole screen area. What can be the problem?
Note: On any item click at f2 I want to replace only f2 with new fragment f4. Keep in mind I have added f1, f2, f3 through xml layout file and adding new fragment f4 programmatically.
我有三个片段,如下图所示。我使用 .xml 文件在 LinearLayout 中添加了所有这三个片段,当我的启动器活动开始时,我使用 setContentView 加载该 .xml 布局。
我对 fragment2 有一些控制。单击任何一个使用 FragmentTransaction 和 commit 方法以编程方式加载 fragment4。这个片段被添加到屏幕上,但问题是程序化添加的 fragment4 占据了整个屏幕区域。可能是什么问题?
注意:在任何项目上单击 f2 我只想用新片段 f4 替换 f2。请记住,我已经通过 xml 布局文件添加了 f1、f2、f3,并以编程方式添加了新的片段 f4。
回答by CodePrimate
You should always add, remove and replace your fragments programatically. As such I suggest you replace your F-1, F-2 and F-3 fragments with containers such as FrameLayout.
您应该始终以编程方式添加、删除和替换您的片段。因此,我建议您用 FrameLayout 之类的容器替换 F-1、F-2 和 F-3 片段。
Basically instead of having a <fragment/>
element as F-1 you make it a <FrameLayout/>
element. Then you perform a fragment transaction in your FragmentActivity's onCreate:
基本上,不是将<fragment/>
元素作为 F-1,而是将其设为<FrameLayout/>
元素。然后在 FragmentActivity 的 onCreate 中执行片段事务:
Fragment1 f1 = new Fragment1();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.f1_container, f1); // f1_container is your FrameLayout container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
Now, Suppose you have done this for F-1, F-2 and F-3. You then replace f2 with f4 by doing the same thing again in your OnClickListener
:
现在,假设您已经为 F-1、F-2 和 F-3 执行了此操作。然后,通过在您的OnClickListener
: 中再次执行相同的操作,将 f2 替换为 f4 :
public void onClick(View v) {
Fragment4 f4 = new Fragment4();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.f2_container, f4); // f2_container is your FrameLayout container
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
ft.addToBackStack(null);
ft.commit();
}
回答by user936414
Keep only FrameLayout as placeholders for the fragment in the XML. In the OnCreate load the fragments in the framelayout. OnClick of the fragment, give that particular FrameLayout's id to replace by Fragment4.
仅保留 FrameLayout 作为 XML 中片段的占位符。在 OnCreate 中加载框架布局中的片段。片段的 OnClick,给那个特定的 FrameLayout 的 id 替换为 Fragment4。