Search code examples
androidandroid-fragmentsandroid-viewpager

FragmentPagerAdapter update title dynamically


I have a ViewPager with FragmentPagerAdapter, which has 3 Fragments. Now, I have a number of items as part of the title of the fragment:

enter image description here

So user can add or remove those items, and title should update accordingly. However, this title seems static.

How can one recall this method to update FragmentPagerAdapter's titles?

@Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0: return items + " (" + notFoundItems + ")";
            case 1: return found;
            case 2: return review;
        }
        return "";
    }

I found a couple of similar questions on SO, but I need to update title, not only the fragment itself.


Solution

  • Finally, I solved it by simply re-setting the pager.

    private OrdersTabsAdapter tabsAdapter;
    @InjectView(R.id.tabs_orders) SlidingTabLayout ordersTabs;
    @InjectView(R.id.pager) ViewPager pager;
    
    public void update() {
      tabsAdapter.updateFragments(productId, status);
      ordersTabs.setViewPager(pager); //this helped with the titles
    }