11

I have a simple messaging application module. In which, there are two swipable tabs. Received and Sent. Let us say I have 20 messages out of which 10 are unread. So what I am doing is showing the tabs as Received - (10). Now when I read the message, it marks the message as read. So I would like to change the title from Received - (10) to Received - (9).

Please let me know how can I do it?

Here is the code which I am using.

@Override
public int getCount() {
    return 2;
}

@Override
public CharSequence getPageTitle(int position) {

    if (position == 0) {
        // if position is zero, set the title to RECEIVED.
        return "Received" + " (" + String.valueOf(intUnreadReceivedMessagesCount) + ")";
    } else {
        // if position is 1, set the title to SENT.
        return "Sent";
    }
}

I am using Pager Sliding Tab Strip as a Pager Tab library. https://github.com/astuetz/PagerSlidingTabStrip

I have tried using notifyDataSetChanged() but for obvious reasons, it does not call it. Any way to resolve the issue. Any better alternative to show and update the count is also welcome.

Thank you.

Bhargav Jhaveri
  • 1,761
  • 1
  • 14
  • 22

3 Answers3

15

It's just a guess without seeing the rest of your code.

notifyDataSetChanged() should be working but there is a trick. You have to override one method in your adapter:

public int getItemPosition(Object item) {
    return POSITION_NONE;
}

This way calling notifyDataSetChanged() will update currently visible page and it's neighbours.

Without it, only new pages are updated.

Update

I looked at linked library. There is a public method notifyDataSetChanged() just like for adapter. So just assign an id for that PagerSlidingTabStrip inside XML and get a reference in your code. Then call:

adapter.notifyDataSetChanged();
tabStrip.notifyDataSetChanged();
LuisComS
  • 350
  • 3
  • 15
Damian Petla
  • 8,453
  • 5
  • 36
  • 44
  • I have tried to this option as well. But it goes to getCount() method only. It does not go inside of getPageTitle(). And also I want to update not only the pages but page titles as well. in the PagerTabStrip which are coming from getPageTitle(int) based on index location. I tried to invalidate the view pager as well. But it did not work. – Bhargav Jhaveri Aug 07 '14 at 20:30
  • Did you try default PagerTitleStrip? Just to make sure that this 3rd lib is not causing the problem. Keep getItemPosition() for testing. – Damian Petla Aug 07 '14 at 20:36
  • Can you also tell which ViewPager adapter implementation are you using? – Damian Petla Aug 07 '14 at 20:37
  • I am extending FragmentStatePagerAdapter to use PagerAdapter. And yeah I tried getItemPosition() as well. But no, it does re invoke the getPageTitle method. – Bhargav Jhaveri Aug 07 '14 at 20:39
7

Accepted answer didn't work for me, using a custom PagerAdapter, and android.support.design.widget.TabLayout for the tabs.

This worked for me:

private void refreshTabTitles() {
    for (int i = 0; i < adapter.getCount(); i++) {
        Tab tab = tabs.getTabAt(i);
        if (tab != null) {
            tab.setText(adapter.getPageTitle(i));
        }
    }
}
marmor
  • 25,207
  • 10
  • 99
  • 145
  • Another way is to call tabLayout.setupWithViewPager(viewPager); every time you need to refresh the text. – Jonas Jul 04 '16 at 06:26
  • @Jonas this reset the tabs to first tab, in this case if you are not on the 0th position this will create issue – Akhil Dad Sep 06 '16 at 11:21
0

Using PagerTabStrip:

I did simply a call from inside adapter's InstantiateItem method to

this.GetPageTitleFormatted(position);

and voilá, it worked!

Ladislav
  • 125
  • 1
  • 8