[英]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.