Skip to content

Instantly share code, notes, and snippets.

@scottyab
Created April 16, 2013 19:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save scottyab/5399115 to your computer and use it in GitHub Desktop.
Save scottyab/5399115 to your computer and use it in GitHub Desktop.
PagerAdapter X of Y. When using a view pager you can simply override the getPageTitle to create a page x of y text as seen in apps like gmail. This was used in a app to swipe through the RSS stories with android.support.v4.app.FragmentPagerAdapter. Alternatively you could use something like https://github.com/ManuelPeinado/NumericPageIndicator
public class MyXofYPagerAdapter extends FragmentPagerAdapter {
private final ArrayList<RssItem> items;
public MyXofYPagerAdapter(FragmentManager fm,
ArrayList<RssItem> items) {
super(fm);
this.items = items;
}
@Override
public int getCount() {
return items.size();
}
@Override
public Fragment getItem(int position) {
RssItem selectedItem = items.get(position);
return MyFragment.newInstance(selectedItem);
}
/**
* depending on position returns X of Y
*/
@Override
public CharSequence getPageTitle(int position) {
if (position < currentPos) {
return getString(R.string.x_of_y_view_pager_prev);
} else if (position > currentPos) {
return getString(R.string.x_of_y_view_pager_next);
} else {
return getString(R.string.x_of_y_view_pager_title,
new Object[] { position + 1, items.size() });
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="x_of_y_view_pager_prev">&lt; Newer</string>
<string name="x_of_y_view_pager_next">Older &gt;</string>
<string name="x_of_y_view_pager_title">%1$d of %2$d</string>
</<resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment