启动时未选择 TabLayout 选择的选项卡图标

TabLayout selected Tab icon is not selected on start up

我在我的应用程序中使用 TabLayout 选项卡式导航。我有一个非常奇怪的问题,我使用以下代码创建了 4 个选项卡:

private int[] tabIcons = {R.drawable.navigation_timeline_icon_selector, R.drawable.navigation_feed_icon_selector,
        R.drawable.navigation_messages_icon_selector, R.drawable.navigation_notification_icon_selector};

 TabLayout tabLayout = setTabLayout();
    if (tabLayout != null) {
        for (int i = 0; i < 4; i++) {
            tabLayout.getTabAt(i).setIcon(tabIcons[i]);
        }
    }

tabIcon 中的每个项目都是一个 selector,具有选中和未选中状态。所有图标选择器配置如下:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
      <item android:drawable="@drawable/navigation_timeline_selected_icon" android:state_selected="true"/>
      <item android:drawable="@drawable/navigation_timeline_selected_icon" android:state_pressed="true"/>
      <item android:drawable="@drawable/navigation_timeline_icon" />
</selector>

问题是当应用程序启动时,第一个选中的选项卡(索引 0)不使用选中状态图标。相反,它使用非选择状态。

为了更清楚地说明,这里是问题的屏幕截图,第一次启动时我的选项卡如下所示:

当它应该是这样的:

在我更改页面后,所有图标都恢复到完整功能,并且正确选择了所选状态。

我尝试使用TabLayout.Tab select()方法,但结果是一样的,使用的图标是未选择的图标。

有人知道我能做些什么来修复它吗?

填充后尝试选择选项卡。

TabLayout tabLayout = setTabLayout();
if (tabLayout != null) {
    for (int i = 0; i < 4; i++) {
        tabLayout.getTabAt(i).setIcon(tabIcons[i]);
    }
    tabLayout.getTabAt(0).select();
}

试试这个:

tabLayout.getTabAt(yourInitialPosition).getCustomView().setSelected(true);

我在 tabLayout 中使用 xml 选择器来选择具有以下状态的图标:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="@drawable/icon_ajuda_off"/>
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="@drawable/icon_ajuda_on"/>
<item android:state_selected="false" android:state_pressed="true" android:drawable="@drawable/icon_ajuda_on"/>
<item android:state_selected="true" android:state_pressed="true" android:drawable="@drawable/icon_ajuda_on"/>

在代码中:

private int[] tabIcons = {R.drawable.ic_tab_sites, R.drawable.ic_tab_help,
        R.drawable.ic_tab_profile, R.drawable.ic_tab_notification, R.drawable.ic_tab_search};

if (tabLayout != null) {
    for (int i = 0; i < 5; i++) {
        tabLayout.getTabAt(i).setIcon(tabIcons[i]);
    }
}

可能会有帮助。

TabLayout 选项卡选择的正确答案是:

TabLayout.Tab currentTab = mTabs.getTabAt(selectedTab);
if (currentTab != null) {
    View customView = currentTab.getCustomView();
    if (customView != null) {
        customView.setSelected(true);
    }
    currentTab.select();
}

其中 currentTab.select() 会将指示器移动到选定的选项卡,当 customView.setSelected() 将使自定义视图中的所有项目设置它们在选择器中的选定状态时看起来已选定。

这是解决方案, 将此代码粘贴到您 onCreate Activity 因为使用 tabs 0 索引不会直接触发这是简单的方法

 viewPager.setCurrentItem(1);
    if (viewPager.getCurrentItem()==1)
    {
        viewPager.setCurrentItem(0);
    }

在我的例子中,我只想将矢量可绘制对象添加到选择器文件,因为它不起作用,所以如果你想使用矢量可绘制对象,你必须将它们添加为单独的文件...

<item android:drawable="@drawable/stnkb_tab_recent_selected_true" android:state_selected="true" />
<item android:drawable="@drawable/stnkb_tab_recent_selected_false" />