简体   繁体   中英

On tab selection change other tabs icons and change the color of the tab text programmatically in android

I have developed tabbedview with custom layouts for each individual tab.Now when I select one tab other tabs icons should change and I want to change the color of the other tabs text. please help?

Here is my code:

          final TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
         tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_one));
         tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_two));
         tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_three));
         tabLayout.addTab(tabLayout.newTab().setCustomView(R.layout.custom_tab_four));


    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final TabViewAdapter adapter = new TabViewAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
    viewPager.setAdapter(adapter);

    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());

            }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {


        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

}
    // - set a tab listener to your tabLayout
    tabLayout.setOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(viewPager) {  
            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                super.onTabUnselected(tab);
            }
            // - this method will get execute if you select some tab
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                super.onTabSelected(tab);
                // - here change the color or image of your tab which is selected and of unselected also
            }
      } 

try to use this tabSelectedTextColor="yourColor" and but you own style on Value-> style

   <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/white"
        app:tabMode="scrollable"
        app:tabGravity="fill"
        app:tabSelectedTextColor="@color/black"
        app:tabTextColor="@color/red" />

You can visit this wonderfull article on Material Tab layout in androidhive.com http://www.androidhive.info/2015/09/android-material-design-working-with-tabs/

Just go through the article and learn how to change Tab icons and color. For changing it on view Pager item, You can set a PageChangeListener in View pager and update the tabs as per new page. If further any query please feel free to comment.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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