Reputation: 6941
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");
}
Upvotes: 0
Views: 3266
Reputation: 30814
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
.
Upvotes: 1