简体   繁体   English

FragmentTabHost更改特定选项卡的片段,即使未选中它

[英]FragmentTabHost changing fragment for a certain tab, even when it's not selected

I have a FragmentTabHost and i have a usertab added like this: 我有一个FragmentTabHost,我有一个添加的用户标签,如下所示:

mTabHost.addTab(mTabHost.newTabSpec("usertab").setIndicator(uti), LoginTab.class, null);

When the user is loggedin i want to change the class for that tab to UserTab which i do like this: 当用户登录后,我想将该选项卡的类更改为UserTab,我这样做是这样的:

getSupportFragmentManager()
                .beginTransaction()
                .replace(android.R.id.tabcontent, new UserTab(), "usertab")
                .commit();

however if the user is on a different tab the content of the tab the user is on gets changed (it's just replacing the content of the tabcontent layout which is connected to the currently selected tab), i was hoping that adding the tag parameter in the replace function would do the trick, but it doesn't (what does it do anyway?, cause it doesn't do anything different then when i don't use it). 但是,如果用户位于其他选项卡上,则用户所在的选项卡上的内容会更改(它只是替换连接到当前所选选项卡的tabcontent布局的内容),我希望在替换功能可以解决这个问题,但是不会(无论如何,它会做什么,因为当我不使用它时,它不会做任何不同的事情)。

Is my only option to programmatically change the tab to the usertab before i commit the transaction or is there a better way? 我是提交交易之前以编程方式将选项卡更改为用户选项卡的唯一选择,还是有更好的方法?

(this is my first toying around with the whole fragment phenomenon by the way) (顺便说一句,这是我第一次玩弄整个碎片现象)

You should change the FragmentTabHost for TabLayout paired with a viewpager , this is a good example ; 您应该将FragmentTabHost for TabLayout更改为与viewpager配对使用,这是一个很好的示例 if you cannot replace the fragmentTabHost i recommend you to do the fragment transactions without the .replace(), because it has unexpected behaviour when popping the backstack transactions which could be the problem youre seeing now 如果您无法替换fragmentTabHost,我建议您不使用.replace()进行碎片交易,因为在弹出后退事务时,它具有意外行为,这可能是您现在看到的问题

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM