Dynamically Update Design Support TabLayout Header

Is there a way to dynamically update the tab title in TabLayout design support? I tried to add a method to the adapter that modifies the contents of the ArrayList array, which contains the tab headers and notifies the adapter, but the tablet names do not change to the new header.

Adapter:

public class TabAdapter extends FragmentPagerAdapter {
    private final List<Fragment> fragments = new ArrayList<>();
    private final List<String> fragmentTiles = new ArrayList<>();

    public TabAdapter(FragmentManager fm) {
        super(fm);
    }

    public void addFragment(Fragment fragment, String title) {
        fragments.add(fragment);
        fragmentTiles.add(title);
    }

    @Override
    public Fragment getItem(int position) {
        return fragments.get(position);
    }

    @Override
    public int getCount() {
        return fragments.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return fragmentTiles.get(position);
    }

    public void setFragmentTiles(int index, String title) {
        fragmentTiles.set(index, title);

        Log.e("ARRAY", fragmentTiles.toString());
    }
}

I am changing the contents of the ArrayList header as follows:

adapter.setFragmentTiles("New title 1");
adapter.setFragmentTiles("New title 2");
adapter.notifyDataSetChanged();

But that does not work. Do I need to update ViewPager or TabLayout?

+4
source share
1 answer

What worked for me was to use the setText method on the tab:

. tab.setText(...);//... CharSequence resId

, TabLayout, index/position:

. tabLayout.getTabAt(...);//... is int.

, notifyDataSetChanged() getCount() , .

, , , .

+4

Source: https://habr.com/ru/post/1612149/


All Articles