Change the tabs text title in dynamic

情到浓时终转凉″ 提交于 2019-12-01 14:40:59

问题


I have a tablayout with four tabs under viewpager. I want to change the text title when i slide the tab. My problem is i can't handle tab respectively.

For instance the four tabs title are 1、2、3、4 , when i slide it will show like 1、9、3、4 or 1、2、9、4...

but it is 7、9、7、7 or 7、7、9、7...in my case

I try to write it on onTabUnselected function . Obviously i don't have the variable to control it.

What can i do next ?

Part of the program:

private ViewPager viewPager;

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayoutHomePage));
        tabLayoutHomePage.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                int id=tab.getPosition();
                switch (id){
                    case 0:
                        tab.setText("9");
                        break;
                    case 1:
                        tab.setText("9");
                        break;
                    case 2:
                        tab.setText("9");
                        break;
                    case 3:
                        tab.setText("9");
                        break;
                }
                    viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                int id=tab.getPosition();
                switch (id){
                    case 0:
                        tab.setText("7");
                        break;
                    case 1:
                        tab.setText("7");
                        break;
                    case 2:
                        tab.setText("7");
                        break;
                    case 3:
                        tab.setText("7");
                        break;
                }
            }

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

            }
        }); 

The original declare four tabs title

tabLayoutHomePage = (TabLayout) view.findViewById(R.id.tabLayoutHomePage);
    tabLayoutHomePage.addTab(tabLayoutHomePage.newTab().setText(R.string.bloodPressure));
    tabLayoutHomePage.addTab(tabLayoutHomePage.newTab().setText(R.string.bloodSugar));
    tabLayoutHomePage.addTab(tabLayoutHomePage.newTab().setText(R.string.bodyWeight));
    tabLayoutHomePage.addTab(tabLayoutHomePage.newTab().setText(R.string.vaccine));
    tabLayoutHomePage.setTabGravity(TabLayout.GRAVITY_FILL);

回答1:


try this

tabLayoutHomePage.getTabAt(position).setText("new Text");



回答2:


This should also work

mSlidingTabLayout.setTabText(position,name)



来源:https://stackoverflow.com/questions/41482145/change-the-tabs-text-title-in-dynamic

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!