I'm trying to dynamically set tab's title from model class. For this, I have overriden the getPageTitle(int position) method then return the charsequence. However as the titles come from the model field, I'm getting duplicates. How can I only return unique field name as page title? Or what is the best way to achieve this.
For example I want to group all books from an author in one tab.
Well, this is how my adapter looks like, so simple:
public class BooksListsAdapter extends FragmentPagerAdapter {
private ArrayList<Books> listOfBooks= new ArrayList<>();
public BooksListsAdapter (FragmentManager fragmentManager, ArrayList<Books> listOfBooks) {
super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
this.listOfBooks= listOfBooks;
}
@Override
public int getCount() {
return listOfBooks.size();
}
@NonNull
@Override
public Fragment getItem(int position) {
return BooksListFragment.newInstance(listOfBooks.get(position).getAuthor(), listOfBooks);
}
@Override
public CharSequence getPageTitle(int position){
return listOfBooks.get(position).getAuthor();// Would like this returns only individual author regardless how many books they have
}
}
This works fine except tabs are created with duplicated titles,hence duplicated contents. However I already have contents grouped in corresponding fragments. I would just like to remove the duplicated tab (the one repeating the same author name).
Thanks for your time.