0

我知道我可以覆盖以下getPageTitle()方法FragmentPagerAdapter

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

...但我想在CursorLoader回调中更改为页面标题:

@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");
}
4

1 回答 1

1

setTitle在您的FragmentPagerAdapter. 这是一个例子:

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();
    }

}

加载程序回调

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();
}

我正在使用的DummyFragment只是一个FrameLayout.

这是一种方式,另一种方式是使用Fragment.getArguments. 像这样的东西:

FragmentPagerAdapter

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

加载程序回调

/**
 * {@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);
    }
}

所以,有几个,但实际上你只需要某种回调到PagerAdapter.getPageTitle.

于 2013-10-26T14:54:02.253 回答