简体   繁体   English

如何使用 ViewPager 在 TabLayout 的选项卡上设置页面标题

[英]How to set page title on TabLayout's tabs with ViewPager

I'm trying to dynamically set tab's title from model class.我正在尝试从 model class 动态设置选项卡的标题。 For this, I have overriden the getPageTitle(int position) method then return the charsequence.为此,我重写了 getPageTitle(int position) 方法,然后返回字符序列。 However as the titles come from the model field, I'm getting duplicates.然而,由于标题来自 model 字段,我得到了重复。 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.谢谢你的时间。

You can make a ViewPager base on the total number of authors you have.您可以根据您拥有的作者总数制作ViewPager 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希望有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 tablayout 中覆盖和显示 viewpager2 中的选项卡标题 - how to override and display title of tabs in viewpager2 in tablayout 滚动viewpager时,TabLayout中的标签标题不会向上滚动 - Tabs title in the TabLayout does not scroll up when the viewpager is scrolled 如何使用TabLayout在Android Viewpager上设置图标 - How to set icons on Android Viewpager With TabLayout 横向中工具栏中带有ViewPager的Android TabLayout标签 - Android tablayout tabs with viewpager in toolbar in landscape 将焦点从 ViewPager/TabLayout 中的选项卡上移开 - Take focus away from tabs in ViewPager/TabLayout 以编程方式更改TabLayout的选项卡,但未更新ViewPager的片段 - Changing tabs of TabLayout programmatically but fragments of ViewPager is not updated 在viewpager中更改选项卡时内容未更新-Tablayout - Content not updating on changing tabs in viewpager - Tablayout 如何在使用ViewPager的TabLayout设置中更改没有动画的页面? - How to change page without animation in a TabLayout setup with ViewPager? Viewpager,如何将当前页面切换到ViewPager中未包含的另一页面,同时将Tablayout保持在底部 - Viewpager , How to switch the current page to another page which is not included in the ViewPager , with keeping the Tablayout at the bottom 如何防止在通过 TabLayoutMediator 初始化期间使用 Viewpager2 加载 Tablayout 中的所有选项卡? - How to prevent loading all Tabs in Tablayout with Viewpager2 during initialization via TabLayoutMediator?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM