gpt4 book ai didi

android - 替换已添加为选项卡主机选项卡的 fragment

转载 作者:太空狗 更新时间:2023-10-29 14:23:33 25 4
gpt4 key购买 nike

我的 android 应用程序出现以下问题:

我有一个包含一些 fragment 作为选项卡的 TabHost。这些 fragment 已以编程方式添加。现在我想用另一个 fragment 替换选项卡的内容。我不知道如何实现这一目标。我 found如何替换通过 xml 添加的 fragment ,但这不是我想要实现的。

提前感谢您的任何建议。

编辑:请求的来源(只有相关部分,如果需要更多请告诉我)

我有一种方法可以初始化 tabhost 并以编程方式添加选项卡/fragment :

private void initialiseTabHost(Bundle args) {
mTabHost = (TabHost)findViewById(android.R.id.tabhost);
mTabHost.setup();

addUserListTab(args);
//methods to add other tabs


mTabHost.setOnTabChangedListener(this);
}

private void addUserListTab(Bundle args){
TabInfo tabInfo = null;
LayoutParams tabIconParams = new LayoutParams(200, 110);
tabIconParams.setMargins(0, 0, 0, 0);
View userIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator,mTabHost, false);
ImageView userIcon = (ImageView) userIndicator.findViewById(R.id.tab_icon);
userIcon.setLayoutParams(tabIconParams);
TabSpec specUser = mTabHost.newTabSpec("user");
specUser.setIndicator(userIndicator);
userIcon.setImageDrawable(getResources().getDrawable(R.drawable.selector_user));
userIcon.setScaleType(ImageView.ScaleType.FIT_CENTER);

AddTab(this, this.mTabHost, specUser, ( tabInfo = new TabInfo("user", ListUserFragment.class, args)));
this.mapTabInfo.put(tabInfo.tag, tabInfo);

}
private static void AddTab(MainActivity activity, TabHost tabHost, TabHost.TabSpec tabSpec, TabInfo tabInfo) {
// Attach a Tab view factory to the spec
tabSpec.setContent(activity.new TabFactory(activity));
tabHost.addTab(tabSpec);
}

问题是我不明白如何替换我以编程方式添加到 tabhost 的 ListUserFragment。当我传递 tabhost 的容器时,如何指定当前四个选项卡中的哪个选项卡应包含新 fragment ,当然应该删除/隐藏旧 fragment 。

最佳答案

您需要获取 FragmentManager 并开始一个 FragmentTransaction 以用另一个替换当前的 Fragment

 getFragmentManager().beginTransaction().replace(R.id.container, new Fragment()).commit();

请务必同时阅读官方 fragment guide. .

关于android - 替换已添加为选项卡主机选项卡的 fragment ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14503496/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com