2015-12-21 55 views
10

我在我的应用中使用TabLayout作为标签导航。 我与它一个很奇怪的问题,我已经创建使用此代码4个选项卡:TabLayout selected启动时未选中标签图标:

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)不使用选择状态的图标。相反,它使用未选择的状态。

更说明这里是问题的一个屏幕截图,在第一次开始我的标签是这样的:

enter image description here

时相反,它应该是这样的:

enter image description here

更改页面后,所有图标都会回到完整功能,并且选择的状态会被正确选择。

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

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

在此先感谢。

+0

不知道如果你已经尝试我建议什么。正如你所说的你已经尝试过select()。 –

+0

可能是一个可绘制的定制问题 –

+0

@MsYvette这是什么意思,它是如何处理的? –

回答

2

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

0

尝试在填充它们之后选择选项卡。

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

就像我说过的:我试图使用TabLayout.Tab select()方法,但结果是相同的,使用的图标是未选择的图标。 –

+0

对不起,我不确定,我在你的问题下留言。我会想一想。 –

+0

@EmilAdz哪一个**是**选定的项目?每次都是一样的吗? –

10

试试这个:

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

+0

我没有“getCustomView()”方法,因为我没有将我的选项卡添加为自定义视图,而是添加为简单的普通图标。所以即使这样做,我也不能使用这个解决方案。 –

+0

getCustomView是TabLayout.Tab类的公共方法,因此您必须拥有它,并且在这种情况下,它将返回您要应用到此选项卡的可绘制选择器。我不明白为什么你不能使用它,如果它的工作。也许我错过了一些东西......你能详细说明一下吗? – emirua

+0

如果您不能使用此解决方案,您可以随时将选择更改为不同的选项卡,然后以编程方式切换回初始位置。 – emirua

3

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

它可能有帮助。

0

这里是解决方案, 的onCreate活动将此代码粘贴在你,因为使用标签0指数没有直接触发,这是简单的方法来做到

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