简体   繁体   English

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

[英]TabLayout selected Tab icon is not selected on start up

I'm using a TabLayout for Tabbed navigation in my app. 我在我的应用程序中使用TabLayout进行选项卡式导航。 I have a really weird issue with it, I have created 4 tabs using this code: 我有一个非常奇怪的问题,我使用此代码创建了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]);
        }
    }

Each of the items in tabIcon is a selector with selected and non-selected states. tabIcon中的每个项目都是具有选定和非选定状态的selector All icon selectors are configured as follows: 所有图标选择器配置如下:

<?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>

The problem is that when the application starts the first selected tab (index 0) does not use the selected state icon. 问题是当应用程序启动时,第一个选定的选项卡(索引0)不使用选定的状态图标。 Instead it uses the non-selected state. 相反,它使用非选择状态。

To be more explanatory here is a screenshot of the issue, on first start my tab looks like this: 更详细的解释是这个问题的截图,首次启动我的选项卡如下所示:

在此输入图像描述

when instead it should be like this: 相反,它应该是这样的:

在此输入图像描述

After I change a page all the icons come back to full functionality, and the selected states are selected properly. 更改页面后,所有图标都恢复为完整功能,并且正确选择了所选状态。

I tried to use the TabLayout.Tab select() method but the result is the same the icon that is used is the not selected icon. 我尝试使用TabLayout.Tab select()方法,但结果与使用的图标是未选择的图标相同。

Does someone know what I can do to fix it? 有人知道我能做些什么来解决它吗?

试试这个:

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

I used in my tabLayout the xml selector for the icons with the following states: 我在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"/>

and in the code: 并在代码中:

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

It might help. 它可能有所帮助。

The correct answer for tab selection in TabLayout would be: TabLayout选项卡选择的正确答案是:

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

where currentTab.select() will move the indicator to the selected tab, when customView.setSelected() will make all the items in the custom view set their selected states from the selectors look selected. 其中currentTab.select()将指示器移动到选定的选项卡,当customView.setSelected()将使自定义视图中的所有项目从选择器中选择所选的状态。

Try selecting the tab after you've populated them. 填写完毕后,请尝试选择该选项卡。

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

here is solution, paste this code in you onCreate Activity because using tabs 0 index not triggers directly this is easy way to do 这里是解决方案,将此代码粘贴到onCreate Activity中,因为使用tabs 0 index不能直接触发这是一种简单的方法

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

In my case I only wanted to add the vector drawable to the selector file by it's not working so if you want to use Vector drawable you MUST add them as separated files... 在我的情况下,我只想将矢量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" />

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

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