TabLayout selected Tab icon is not selected on start up

前端 未结 6 435
小鲜肉
小鲜肉 2021-01-17 14:14

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[         


        
相关标签:
6条回答
  • 2021-01-17 14:54

    here is solution, paste this code in you onCreate Activity because using tabs 0 index not triggers directly this is easy way to do

     viewPager.setCurrentItem(1);
        if (viewPager.getCurrentItem()==1)
        {
            viewPager.setCurrentItem(0);
        }
    
    0 讨论(0)
  • 2021-01-17 14:59

    Try this:

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

    0 讨论(0)
  • 2021-01-17 15:02

    I used in my tabLayout the xml selector for the icons with the following states:

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

    0 讨论(0)
  • 2021-01-17 15:03

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

    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.

    0 讨论(0)
  • 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();
    }
    
    0 讨论(0)
  • 2021-01-17 15:13

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

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

    0 讨论(0)
提交回复
热议问题