0

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.

esQmo_
  • 1,464
  • 3
  • 18
  • 43

1 Answers1

1

You can make a ViewPager base on the total number of authors you have. And for each page, you will display all the books of this author.

public class BooksListsAdapter extends FragmentPagerAdapter {
    private List<String> authors = new ArrayList<>();
    private HashMap<String, List<Books>> booksMap = new HashMap<>(); // author is key, list of book of this author is value

    public BooksListsAdapter(FragmentManager fragmentManager, ArrayList<Books> listOfBooks) {
        super(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);

        setBooksWithAuthor(listOfBooks);
        // LOGIC to order tab go here. Tab order <=> authors list order
        // Example: Order tab by author name: Collections.sort(authors);
    }

    private void setBooksWithAuthor(ArrayList<Books> books) {
        for (Books book : books) {
            if (booksMap.containsKey(book.getAuthor())) {
                booksMap.get(book.getAuthor()).add(book);
            } else {
                booksMap.put(book.getAuthor(), new ArrayList<Books>() {{
                    add(book);
                }});
                authors.add(book.getAuthor());
            }
        }
    }

    @Override
    public int getCount() {
        return authors.size(); // total page = total author
    }

    @NonNull
    @Override
    public Fragment getItem(int position) {
        String author = authors.get(position);
        return BooksListFragment.newInstance(author, booksMap.get(author));
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return authors.get(position);
    }
}

Hope it help

Linh
  • 57,942
  • 23
  • 262
  • 279
  • Thank you for your quick answer. I'm going to test it then I'll come back to you. – esQmo_ Jun 26 '20 at 02:28
  • hello, can you please help me with this question. https://stackoverflow.com/questions/62587076/sharepreference-to-store-int-value please? – Dibas Dauliya Jun 26 '20 at 02:31
  • The newInstance method expects an ArrayList,should I convert the List into an ArrayList? – esQmo_ Jun 26 '20 at 02:46
  • you should change in your BooksListFragment, replace ArrayList by List – Linh Jun 26 '20 at 02:51
  • Worked flawlessly! Thank you and your time, I really appreciated it – esQmo_ Jun 26 '20 at 02:53
  • @PhanVanLinh would you mind helping on a different, quite similar issue? – esQmo_ Jun 26 '20 at 03:23
  • yes, but if it not relevant to this issue, please post a new question because maybe I don't know and other people can help you – Linh Jun 26 '20 at 03:24
  • Hey, how can I set ordering? I mean I would like to have some logic so that the tab with recent item be at first place (position 1) – esQmo_ Jun 28 '20 at 15:10
  • @esQmo_, you can set the ordering by ordering the `authors` array – Linh Jun 29 '20 at 01:39
  • Please help on that one. Not really experienced – esQmo_ Jun 29 '20 at 01:41
  • 1
    @esQmo_ updated the answer, please see inside BooksListsAdapter constructor, hope you can get the idea – Linh Jun 29 '20 at 01:50
  • For the other question, I just asked it here, would you take a look if you get a chance? https://stackoverflow.com/questions/62661890/how-to-set-page-titles-on-tablayouts-tabs-from-nested-objects?noredirect=1&lq=1 – esQmo_ Jul 01 '20 at 10:51