Android的 - 动态添加片段 [英] Android - adding fragments dynamically

查看:171
本文介绍了Android的 - 动态添加片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看过了互联网附近,我发现Android的片段(fragments),在那里它们被放置在位置(像他们两个,一个作为一个列表,二是从列表diplaying所选项目的详细信息)的唯一例子。我的目标是有经典的Andr​​oid的活动,我希望能够加入到其动态$特别GUI p $ p型$ P ​​$ ppared片段。我想补充有圆顶的EditText元素(editTextFragment),或几个单选按钮(r​​adionbuttonFragment)的组。是否有如何做到这一点的任何例子吗?制造稳定的片段是无法使用我。

I have looked around the internet and I have found only examples of Android fragmens, where they are placed on place (like two of them, one as a list, second is diplaying details of a selected item from list). My goal is to have classic android activity and I want to be able to add to it dynamically pre-prepared fragments with particular GUI. I want to add there dome group of EditText elements (editTextFragment), or several radiobuttons (radionbuttonFragment). Is there any example of how to do that? Making stable fragments is not usable for me.

感谢

推荐答案

这答案是为那些谁所有搜索答案同样的问题。 答案由FragmentManager和FragmentTransaction以下语法来实现的。

This answer is for those who all searching the answer for same question. The answer is achieved by FragmentManager and FragmentTransaction with the following syntax.

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

那么你需要的片段添加到此fragmentTransaction使用以下语法。

then you need to add your fragment to this fragmentTransaction with the following syntax.

fragmentTransaction.replace(R.id.detailFragment, layout1);

最后,你必须提交事务。否则,所做的更改不会持续。

And finally you MUST commit your transaction. Otherwise changes will not persist.

fragmentTransaction.commit();

有关简短的例子来看看这个博客帖子。我希望这会帮助你。

For brief example see this blog post. I hope This will help you.

感谢你。

这篇关于Android的 - 动态添加片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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