启动时未选中TabLayout选中的选项卡图标 [英] TabLayout selected Tab icon is not selected on start up

查看:973
本文介绍了启动时未选中TabLayout选中的选项卡图标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm using a TabLayout for Tabbed navigation in my app. I have a really weird issue with it, I have created 4 tabs using this code:

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中的每个项目都是选择器具有选定和未选中状态。所有图标选择器配置如下:

Each of the items in tabIcon is a selector with selected and non-selected states. 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>

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

The problem is that when the application starts the first selected tab (index 0) does not use the selected state icon. 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:

相反它应该是这样的:

更改页面后,所有图标都恢复全部功能,并且正确选择了所选状态。

After I change a page all the icons come back to full functionality, and the selected states are selected properly.

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

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.

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

Does someone know what I can do to fix it?

推荐答案

选项卡选择的正确答案 TabLayout 中的离子将是:

The correct answer for tab selection in TabLayout would be:

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()将使自定义视图中的所有项目从选择器中选择的所选状态设置为$ c>时,将指示器移动到所选选项卡。

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.

这篇关于启动时未选中TabLayout选中的选项卡图标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆