欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

TabLayout关联ViewPager后不展示标题的原因分析

程序员文章站 2023-12-24 20:26:39
...
TabLayout
setupWithViewPager(Viewpage viewPage)
setupWithViewPager(ViewPage viewPage,boolean b);
//源码:重点部分加粗
private void setupWithViewPager(@Nullable ViewPager viewPager, boolean autoRefresh, boolean implicitSetup) {
    if (this.viewPager != null) {
        if (this.pageChangeListener != null) {
            this.viewPager.removeOnPageChangeListener(this.pageChangeListener);
        }

        if (this.adapterChangeListener != null) {
            this.viewPager.removeOnAdapterChangeListener(this.adapterChangeListener);
        }
    }

    if (this.currentVpSelectedListener != null) {
        this.removeOnTabSelectedListener(this.currentVpSelectedListener);
        this.currentVpSelectedListener = null;
    }

    if (viewPager != null) {
        this.viewPager = viewPager;
         if (this.pageChangeListener == null) {
            this.pageChangeListener = new TabLayout.TabLayoutOnPageChangeListener(this);
        }

        this.pageChangeListener.reset();
        viewPager.addOnPageChangeListener(this.pageChangeListener);
        this.currentVpSelectedListener = new TabLayout.ViewPagerOnTabSelectedListener(viewPager);
        this.addOnTabSelectedListener(this.currentVpSelectedListener);
        PagerAdapter adapter = viewPager.getAdapter();
        if (adapter != null) {
            this.setPagerAdapter(adapter, autoRefresh);
        }

        if (this.adapterChangeListener == null) {
            this.adapterChangeListener = new TabLayout.AdapterChangeListener();
        }

        this.adapterChangeListener.setAutoRefresh(autoRefresh);
        viewPager.addOnAdapterChangeListener(this.adapterChangeListener);
        this.setScrollPosition(viewPager.getCurrentItem(), 0.0F, true);
    } else {
        this.viewPager = null;
        this.setPagerAdapter((PagerAdapter)null, false);
    }

    this.setupViewPagerImplicitly = implicitSetup;
}
void setPagerAdapter(@Nullable PagerAdapter adapter, boolean addObserver) {
    if (this.pagerAdapter != null && this.pagerAdapterObserver != null) {
        this.pagerAdapter.unregisterDataSetObserver(this.pagerAdapterObserver);
    }

    this.pagerAdapter = adapter;
    if (addObserver && adapter != null) {
        if (this.pagerAdapterObserver == null) {
            this.pagerAdapterObserver = new TabLayout.PagerAdapterObserver();
        }

        adapter.registerDataSetObserver(this.pagerAdapterObserver);
    }

    this.populateFromPagerAdapter();
}

void populateFromPagerAdapter() {
    //移除所有的tad
    this.removeAllTabs();
    if (this.pagerAdapter != null) {
        int adapterCount = this.pagerAdapter.getCount();

        int curItem;
        for(curItem = 0; curItem < adapterCount; ++curItem) {
           //重新添加tab
            this.addTab(this.newTab().setText(this.pagerAdapter.getPageTitle(curItem)), false);
        }

        if (this.viewPager != null && adapterCount > 0) {
            curItem = this.viewPager.getCurrentItem();
            if (curItem != this.getSelectedTabPosition() && curItem < this.getTabCount()) {
                this.selectTab(this.getTabAt(curItem));
            }
        }
    }

}

原因就在源码中,tabLayout的标题被重置为viewPager的adapter的getPageTitle();

 

 

 

相关标签: TabLayout

上一篇:

下一篇: