Dynamically change the title of a android.support.v4.view.PagerTitleStrip
Asked Answered
P

2

6

I am currentky using a PagerTitleStrip in my application (Doc: http://developer.android.com/reference/android/support/v4/view/PagerTitleStrip.html)

Everything works fine, and I have set the title like this:

public class PageAdapter extends FragmentPagerAdapter {

    private List<Fragment> fragments;

    public PageAdapter(FragmentManager fm, List<Fragment> fragments) {
        super(fm);
        this.fragments = fragments;
    }

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

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

    String[] titles = { "1", "2", "3", "4","5" };

    @Override
    public CharSequence getPageTitle(int position) {
        return titles[position];
    }
}

My concern now, is that once the first Fragment has finished loading, I have to change the title of the strip.

I have already tried to get the FragmentPagerAdapter and tried to manipulate it, but never succeed to change a title (text)

There is indeed a getPageTitle method: mPagerAdapter.getPageTitle(position) but no setPageTitle ...

Any idea or suggestion?

Piercing answered 10/3, 2013 at 10:15 Comment(4)
Have you tried to use PagerAdapter.notifyDataSetChanged()?Ptomaine
@kape123 I think he doesn't know how to change the hardcoded array to something else.Simpson
@kape123 His last statement leads me to believe otherwise There is indeed a getPageTitle method: mPagerAdapter.getPageTitle(position) but no setPageTitleSimpson
@kape123 you are right, I must be tired: notifyDataSetChanged works perfectly, I just need to use getActivity in my fragment, modify titles and use notifyDataSetChanged. Please add your answer, so I will accept it.Piercing
P
12

If I understand problem correctly - you get data later and then want to update titles array and trigger refresh of titles.

If that's the case just trigger PagerAdapter.notifyDataSetChanged(); after that method is invoked Fragments will call getPageTitle method and update themselves.

Ptomaine answered 10/3, 2013 at 10:33 Comment(1)
This works nicely but I believe it has the downside to reload all the pages : not so good for performance.Pompey
P
-1

Kind of ugly solution but working : iterate to the visual tree and set the text yourself.

Also, you have to be aware that there is a first transparent/empty item on the left.

Here is the code (Xamarin here) to update the first title.

var firstHidedOneFound = false;
for (int i = 0; i < _tabstrip.ChildCount; ++i)
{
    var child = _tabstrip.GetChildAt(i) as TextView;

    if (child == null) { continue; }
    if (!firstHidedOneFound)
    {
        firstHidedOneFound = true;

        continue;
    }
    child.Text = ViewModel.MyShowPanelTitle;
    break;
}
Pompey answered 24/8, 2017 at 8:16 Comment(1)
Why downvoting without explanation ? I would like to improve my skills :)Pompey

© 2022 - 2024 — McMap. All rights reserved.