0

I know that I can override the getPageTitle() method of FragmentPagerAdapter:

@Override
public CharSequence getPageTitle(int position) {
    return "myTitle";
}

...but I'd like to change to page titles on a CursorLoader callback:

@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
            // How to set the page title here? I miss something like:
            //     mPagerTitleStrip.setTitle(1, "My Title");
            // or: mFragmentPagerAdapter.setTitle(1, "My Title");
}
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
cody
  • 6,389
  • 15
  • 52
  • 77
  • 1
    In a hacky attempt you could simply reference the `PagerTitleStrip` and iterate over it's children(which should be 3) and cast them to `TextViews` and set the text directly. Why do you want to set the titles so fast? – user Oct 26 '13 at 14:09
  • I do an instant search and show the number of search results displayed on each fragment as part of its title. – cody Oct 29 '13 at 21:11

1 Answers1

1

Create your own setTitle method in your FragmentPagerAdapter. Here's an example:

FragmentPagerAdapter

public class PagerAdapter extends FragmentPagerAdapter {

    /** A @ {@link List} of each {@link Fragment} displayed */
    private final List<Fragment> mFragments = new ArrayList<Fragment>();
    /** The titles for the underlying {@link PagerTitleStrip} */
    private final List<String> mTitles = new ArrayList<String>();

    /**
     * Constructor for <code>PagerAdapter</code>
     * 
     * @param fm The {@link FragmentManager} to use
     */
    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public Fragment getItem(int position) {
        return mFragments.get(position);
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public int getCount() {
        return mFragments.size();
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public CharSequence getPageTitle(int position) {
        return mTitles.get(position);
    }

    /**
     * Adds a new {@link Fragment} and title to the adapter
     * 
     * @param fragment The {@link Fragment} to add
     * @param title The title for the {@link Fragment}
     */
    public void add(Fragment fragment, String title) {
        mFragments.add(fragment);
        mTitles.add(title);
        notifyDataSetChanged();
    }

    /**
     * Removes each {@link Fragment} from the adapter
     */
    public void clear() {
        mFragments.clear();
        mTitles.clear();
        notifyDataSetChanged();
    }

}

LoaderCallback

private PagerAdapter mAdapter;


/**
 * {@inheritDoc}
 */
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
    final Uri uri = Audio.Artists.EXTERNAL_CONTENT_URI;
    final String[] projection = new String[] {
            BaseColumns._ID, ArtistColumns.ARTIST
    };
    return new CursorLoader(this, uri, projection, null, null, null);
}

/**
 * {@inheritDoc}
 */
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    while (data != null && data.moveToNext()) {
        final String artist = data.getString(1);
        mAdapter.add(new DummyFragment(), artist);
    }
}

/**
 * {@inheritDoc}
 */
@Override
public void onLoaderReset(Loader<Cursor> loader) {
    mAdapter.clear();
}

The DummyFragment I'm using is nothing but a FrameLayout.

That's one way, another would be to use Fragment.getArguments. Something like this:

FragmentPagerAdapter

/**
 * {@inheritDoc}
 */
@Override
public CharSequence getPageTitle(int position) {
    return getItem(position).getArguments().getString("your_key");
}

LoaderCallback

/**
 * {@inheritDoc}
 */
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
    while (data != null && data.moveToNext()) {
        final String artist = data.getString(1);
        final Bundle args = new Bundle();
        args.putString("your_key", artist);
        final DummyFragment fragment = new DummyFragment();
        fragment.setArguments(args);
        mAdapter.add(fragment);
    }
}

So, there's a couple, but really all you need is some sort of callback to PagerAdapter.getPageTitle.

adneal
  • 30,484
  • 10
  • 122
  • 151
  • Thanks for your quick response. But that wasn't what I'm looking for, maybe I was not clear enough... my problem is that the titles are updated when getPageTitle(int position) is called, and thats too late. I need to set the title really INSTANTLY. – cody Oct 29 '13 at 21:06
  • Any other suggestion? – cody Oct 30 '13 at 10:46