viewList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
tb_inspection_facility_room.addTab(tb_inspection_facility_room.newTab().setText("tab"+i));
View view = inflater.from(this).inflate(R.layout.inspection_viewpager_facility_page,null);
TextView textView = view.findViewById(R.id.textview);
textView.setText("Page"+i);
}
pagerAdapter = new InspectionRoomPagerAdapter(viewList);
vp_inspection_facility_room.setAdapter(pagerAdapter);
tb_inspection_facility_room.setupWithViewPager(vp_inspection_facility_room);
复制代码
在设置setupWithViewPager后标题没有如愿的显示出来,其实是Tablayout调用viewpager的getTItle方法的时候导致没有数据展示出来,源码:
addTab(newTab().setText(mPagerAdapter.getPageTitle(i)), false);
复制代码
解决方法就是在pagerAdapter里的
@Nullable
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
复制代码
返回你想要的标题就行了
解决后:
viewList = new ArrayList<>();
List<String> titleList = new ArrayList<>();
for (int i = 0; i < 3; i++) {
titleList.add("title"+i);
tb_inspection_facility_room.addTab(tb_inspection_facility_room.newTab().setText("tab"+i));
View view = inflater.from(this).inflate(R.layout.inspection_viewpager_facility_page,null);
TextView textView = view.findViewById(R.id.textview);
textView.setText("Page"+i);
viewList.add(view);
}
pagerAdapter = new InspectionRoomPagerAdapter(viewList,titleList);
vp_inspection_facility_room.setAdapter(pagerAdapter);
tb_inspection_facility_room.setupWithViewPager(vp_inspection_facility_room);
复制代码
转载于:https://juejin.im/post/5be17fe8e51d45544c1b2f0e
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/101334.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...