如何使用 ViewPager 在 TabLayout 的选项卡上设置页面标题
How to set page title on TabLayout's tabs with ViewPager
我正在尝试根据模型 class 动态设置选项卡的标题。为此,我覆盖了 getPageTitle(int position) 方法,然后 return 字符序列。但是,由于标题来自模型字段,所以我得到了重复项。我怎样才能只 return 唯一字段名称作为页面标题?或者实现这一目标的最佳方法是什么。
例如,我想将一位作者的所有书籍集中在一个选项卡中。
好吧,这就是我的适配器的样子,非常简单:
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
}
}
这工作正常,除了标签是用重复的标题创建的,因此是重复的内容。但是我已经将内容分组到相应的片段中。我只想删除重复的标签(重复相同作者姓名的标签)。
谢谢你的时间。
您可以根据您拥有的作者总数进行ViewPager
。对于每一页,您将显示该作者的所有书籍。
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);
}
}
希望对您有所帮助
我正在尝试根据模型 class 动态设置选项卡的标题。为此,我覆盖了 getPageTitle(int position) 方法,然后 return 字符序列。但是,由于标题来自模型字段,所以我得到了重复项。我怎样才能只 return 唯一字段名称作为页面标题?或者实现这一目标的最佳方法是什么。
例如,我想将一位作者的所有书籍集中在一个选项卡中。
好吧,这就是我的适配器的样子,非常简单:
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
}
}
这工作正常,除了标签是用重复的标题创建的,因此是重复的内容。但是我已经将内容分组到相应的片段中。我只想删除重复的标签(重复相同作者姓名的标签)。
谢谢你的时间。
您可以根据您拥有的作者总数进行ViewPager
。对于每一页,您将显示该作者的所有书籍。
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);
}
}
希望对您有所帮助