Android TabLayout setupWithViewPager方法绑定Viewpager不显示文字
public void setupWithViewPager(@Nullable ViewPager viewPager) {
setupWithViewPager(viewPager, true);
}
public void setupWithViewPager(@Nullable final ViewPager viewPager, boolean autoRefresh) {
setupWithViewPager(viewPager, autoRefresh, false);
}
private void setupWithViewPager(
@Nullable final ViewPager viewPager, boolean autoRefresh, boolean implicitSetup) {
···
if (viewPager != null) {
this.viewPager = viewPager;
···
//获取viewPager PagerAdapter
final PagerAdapter adapter = viewPager.getAdapter();
if (adapter != null) {
setPagerAdapter(adapter, autoRefresh);
}
}
void setPagerAdapter(@Nullable final PagerAdapter adapter, final boolean addObserver) {
if (pagerAdapter != null && pagerAdapterObserver != null) {
// If we already have a PagerAdapter, unregister our observer
pagerAdapter.unregisterDataSetObserver(pagerAdapterObserver);
}
pagerAdapter = adapter;
if (addObserver && adapter != null) {
// Register our observer on the new adapter
if (pagerAdapterObserver == null) {
pagerAdapterObserver = new PagerAdapterObserver();
}
adapter.registerDataSetObserver(pagerAdapterObserver);
}
// Finally make sure we reflect the new adapter
populateFromPagerAdapter();
}
···
//重点
void populateFromPagerAdapter() {
removeAllTabs();
if (pagerAdapter != null) {
final int adapterCount = pagerAdapter.getCount();
for (int i = 0; i < adapterCount; i++) {
addTab(newTab().setText(pagerAdapter.getPageTitle(i)), false);
}
// Make sure we reflect the currently set ViewPager item
if (viewPager != null && adapterCount > 0) {
final int curItem = viewPager.getCurrentItem();
if (curItem != getSelectedTabPosition() && curItem < getTabCount()) {
selectTab(getTabAt(curItem));
}
}
}
}
}