【问题标题】:TabLayout selected Tab icon is not selected on start upTabLayout selected 启动时未选择选项卡图标
【发布时间】:2019-03-11 20:58:11
【问题描述】:

我在我的应用程序中使用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() 方法但结果相同,使用的图标是未选中的图标。

有人知道我可以做些什么来解决它吗?

【问题讨论】:

  • @MsYvette 这是什么意思,如何处理?
  • @MsYvette 你想看选择器吗?
  • 是的,看起来你是对的,我想我的选择器中缺少一个状态,现在正在测试它。
  • @MsYvette 很好,选择器的更改并没有解决问题,由于某种原因它仍未被选中。继续寻找解决方案。
  • @MsYvette 选择器代码已添加。

标签: android tabs icons android-tablayout android-selector


【解决方案1】:

试试这个:

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

【讨论】:

  • 我没有“getCustomView()”方法,因为我没有将标签添加为自定义视图,而是添加为简单的普通图标。因此,即使这可行,我也无法使用此解决方案。
  • getCustomView 是 TabLayout.Tab 类的公共方法,因此您必须拥有它,在这种情况下,它将返回您应用到此选项卡的可绘制选择器。我不明白为什么如果它有效,你就不能使用它。也许我错过了一些东西..你能详细说明一下吗?
  • 如果您不能使用此解决方案,您可以随时将选择更改为不同的选项卡,然后以编程方式切换回初始位置。
  • 你说得对,getCustomView 确实是一个公共方法,但除非你将标签设置为自定义视图而不是图标标签,否则你不能使用该方法。
  • @Emil 所以你可以试试 tabLayout.getTabAt(0).select();虽然如果你有自定义视图,它就不起作用。
【解决方案2】:

我在我的 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]);
    }
}

这可能会有所帮助。

【讨论】:

    【解决方案3】:

    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() 将使自定义视图中的所有项目从选择器中设置它们的选定状态看起来已选定。

    【讨论】:

      【解决方案4】:

      填充后尝试选择选项卡。

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

      【讨论】:

      • 就像我说的:我尝试使用 TabLayout.Tab select() 方法但结果相同,使用的图标是未选择的图标。
      • 是的,现在看到了...实际上我认为默认情况下也选择了第一个选项卡。问题不在于选项卡没有被选中,而是使用了错误的图标。
      • 这个问题是什么意思?每次选择另一个选项卡时。启动时应选择 0 索引选项卡。
      【解决方案5】:

      这里有解决方案, 将此代码粘贴到您的 onCreate 活动中,因为使用选项卡 0 索引不会直接触发,这是一种简单的方法

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

      【讨论】:

        【解决方案6】:

        在我的情况下,我只想将矢量 drawable 添加到选择器文件中,因为它不起作用,所以如果你想使用 Vector drawable,你必须将它们添加为单独的文件...

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

        【讨论】: