首页 > 解决方案 > TabHost:切换选项卡后第一个加载的选项卡保持在后台

问题描述

编辑: 当我使用 TabLayout+ViewPager 时发生了同样的事情。


我的一个片段有一个带有 4 个选项卡的 TabHost。以下情况仅在深色主题激活 ( AppCompatDelegate.MODE_NIGHT_YES) 时发生。

当片段第一次显示并且我更改选项卡时,会出现鬼影效果并且第一个选项卡留在后台:

在此处输入图像描述

哪个选项卡是默认选项卡以及单击哪个选项卡都没有关系 - 任何组合都会导致:

在此处输入图像描述

在此处输入图像描述

当我退出片段并返回时,就可以了。

这是组件树: 在此处输入图像描述

我这样设置 TabHost:

    TabHost host = view.findViewById(R.id.tabHost);
    host.setup();

    TabHost.TabSpec spec = host.newTabSpec("one");
    spec.setContent(R.id.tab1);
    spec.setIndicator("", getResources().getDrawable(R.drawable.outline_info_white_24));
    host.addTab(spec);

    //Tab 2
    spec = host.newTabSpec("two");
    spec.setContent(R.id.tab2);
    spec.setIndicator("", getResources().getDrawable(R.drawable.outline_account_balance_white_24));
    host.addTab(spec);

    //Tab 3
    spec = host.newTabSpec("three");
    spec.setContent(R.id.tab3);
    spec.setIndicator("", getResources().getDrawable(R.drawable.outline_notifications_none_white_24));
    host.addTab(spec);

    //Tab 4
    spec = host.newTabSpec("four");
    spec.setContent(R.id.tab4);
    spec.setIndicator("", getResources().getDrawable(R.drawable.outline_public_white_24));
    host.addTab(spec);

是什么导致了这种情况以及如何解决?似乎第一个加载的选项卡停留在后台。

标签: androidlistviewandroid-tabhost

解决方案


Yes... I face this problem too.

you have to try to add layout you need to inflate(replace) layout not add. if you add then when you swipe layout add to another layout to current layout. just replace layout.

 FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.main, onlinesong_home); //this line
        ft.commit();

Happy coding!!!


推荐阅读