FragmentPagerAdapter update title dynamically
you have 3 fragments so you will use 3 tabs(first tab hav index 0,second 1,third 2). get your tab using index and set her text as title. May this help you bro
Example:
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.getTabAt(index).setText("");
Okay. So you can have a method in FragmentPagerAdapter
that will update the count for notFoundItems
, foundItems
, itemsForReview
. After you update them, call the notifyDataSetChanged()
.
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0: return items + " (" + notFoundItems + ")";
case 1: return found + " (" + foundItems + ")";
case 2: return review + " (" + itemsForReview + ")";
}
return "";
}
Also, add this to the your implementation of FragmentPagerAdapter
class. So whenever you have a new data count and want to update the title, call this method.
public void updateTitleData(int notFoundItems, int foundItems, int itemsForReview) {
this.notFoundItems = notFoundItems;
this.foundItems = foundItems;
this.itemsForReview = itemsForReview;
notifyDataSetChanged();
}
Finally, I solved it by simply re-setting the pager.
private OrdersTabsAdapter tabsAdapter;
@InjectView(R.id.tabs_orders) SlidingTabLayout ordersTabs;
@InjectView(R.id.pager) ViewPager pager;
public void update() {
tabsAdapter.updateFragments(productId, status);
ordersTabs.setViewPager(pager); //this helped with the titles
}
As one of the comments beneath it suggests, the accepted answer looks a bit overkill to me.
Based on Frank's answer, this is what I have done:
public void myUpdatePageTitle(int pagePosition, int numItems) {
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
TabLayout.Tab tab = tabLayout.getTabAt(pagePosition);
if (tab != null) {
tab.setText(myCalcTabTitle(pagePosition, numItems));
}
}
private String myCalcTabTitle(int pagePosition, int numItems) {
//TODO - For this page, return whatever title string you like, including the numItems value. (The best way to do this is using a string resource that accepts a decimal parameter.)
}