Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change the tabs text title in dynamic

Tags:

android

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);
like image 790
Morton Avatar asked Jan 05 '17 10:01

Morton


1 Answers

try this

tabLayoutHomePage.getTabAt(position).setText("new Text");
like image 88
Mureed Hussain Avatar answered Oct 19 '22 22:10

Mureed Hussain