How to update view pager item TITLE dynamically
Asked Answered
P

3

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.

Parole answered 7/8, 2014 at 20:0 Comment(0)
T
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();
Thurnau answered 7/8, 2014 at 20:26 Comment(4)
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.Parole
Did you try default PagerTitleStrip? Just to make sure that this 3rd lib is not causing the problem. Keep getItemPosition() for testing.Thurnau
Can you also tell which ViewPager adapter implementation are you using?Thurnau
I am extending FragmentStatePagerAdapter to use PagerAdapter. And yeah I tried getItemPosition() as well. But no, it does re invoke the getPageTitle method.Parole
D
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));
        }
    }
}
Desegregate answered 5/6, 2016 at 17:1 Comment(2)
Another way is to call tabLayout.setupWithViewPager(viewPager); every time you need to refresh the text.Chemurgy
@Chemurgy this reset the tabs to first tab, in this case if you are not on the 0th position this will create issueBateman
P
0

Using PagerTabStrip:

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

this.GetPageTitleFormatted(position);

and voilá, it worked!

Pretend answered 9/4, 2017 at 12:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.