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?
source
share