Using TabLayout inside a Fragment; tab text invisible

前端 未结 5 1726
迷失自我
迷失自我 2020-12-08 03:00

I\'m currently experimenting with various new components in the new Android Support Design library. I\'ve implemented a NavigationView in my MainActivity.

相关标签:
5条回答
  • 2020-12-08 03:21

    The developer working on this library recommends the following work-a-round:

    if (ViewCompat.isLaidOut(tabLayout)) {
            tabLayout.setupWithViewPager(viewPager);
        } else {
            tabLayout.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
                @Override
                public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
                    tabLayout.setupWithViewPager(viewPager);
                    tabLayout.removeOnLayoutChangeListener(this);
                }
            });
        }
    
    0 讨论(0)
  • 2020-12-08 03:32

    Lack reputation to comment @arif ardiansyah...

    Check the source code:

     `TabLayout#setupWithViewPager(...)` 
    
     -->>`setPagerAdapter(adapter, true);`
    
     -->>`populateFromPagerAdapter();`
    

    Here, TabLayout removes all tabs and recreates some new tabs whose text are set with PagerAdapter#getPageTitle(...). So you can override this method to provide Tablayout with your tabs' title.

    0 讨论(0)
  • 2020-12-08 03:36

    just tried to override the method in adapter:

    public CharSequence getPageTitle(int position) {}

    and it works.

    0 讨论(0)
  • 2020-12-08 03:40

    Update: Design Library v23.0.0 solves this issue.


    Found the problem here: https://code.google.com/p/android/issues/detail?id=180462

    Simple workaround:

    tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
    
    0 讨论(0)
  • 2020-12-08 03:42

    I face the same problem with new version of android support Design Library v23.1.1. But I just figure it out what happening.

    if your view pager has 2 fragment as your mention above, then you set your view pager with this statement:

    tabLayout.setupWithViewPager(viewPager);
    

    automatically you will have 2 tabs with empty text. So you should not add new tab anymore. But something you have to do is to set text in that two created tabs.

    tabLayout.getTabAt(0).setText("Campusplan");
    tabLayout.getTabAt(1).setText("Raumplan");
    

    Conclusion. To make tabs work as you want, important thing you have to do :

    tabLayout.setupWithViewPager(viewPager);
    tabLayout.getTabAt(0).setText("Campusplan");
    tabLayout.getTabAt(1).setText("Raumplan");
    

    I hope this will be helpful for you :)

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