TabLayout(二)

当进行 tablayout 切换时的处理流程
在 TabLayout.setupWithViewPager(vp)过程中
我们通过 TabLayout.pageChangeListener 获取 viewpager 的翻页事件获取页码 position
对当前 TabLayout 添加一个为 ViewPager 准备的 OnTabSelectedListener

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);
        }

我们正常对 tablayout 的切换操作是通过点击对应 tab 来进行,所以来看 tabview 的点击监听

        public boolean performClick() {
            boolean handled = super.performClick();
            if (this.tab != null) {
                if (!handled) {
                    this.playSoundEffect(0);
                }

                this.tab.select();
                return true;
            } else {
                return handled;
            }
        }
//进入到 tab.select();
        public void select() {
            if (this.parent == null) {
                throw new IllegalArgumentException("Tab not attached to a TabLayout");
            } else {
                this.parent.selectTab(this);
            }
        }
//然后进入到 TabLayout.selectTab
    void selectTab(TabLayout.Tab tab, boolean updateIndicator) {
        TabLayout.Tab currentTab = this.selectedTab;
        if (currentTab == tab) {//与上一次选中的 tab相同
            if (currentTab != null) {
//遍历所有的 onTabSelectedListener 监听并进行回调
                this.dispatchTabReselected(tab);
//执行动画
                this.animateToTab(tab.getPosition());
            }
        } else {//与上一次选中的不同
            int newPosition = tab != null ? tab.getPosition() : -1;
            if (updateIndicator) {
                if ((currentTab == null || currentTab.getPosition() == -1) && newPosition != -1) {
                    this.setScrollPosition(newPosition, 0.0F, true);
                } else {
                    this.animateToTab(newPosition);
                }

                if (newPosition != -1) {
                    this.setSelectedTabView(newPosition);
                }
            }

            this.selectedTab = tab;
            if (currentTab != null) {
                this.dispatchTabUnselected(currentTab);
            }

            if (tab != null) {
                this.dispatchTabSelected(tab);
            }
        }

    }

根据选中的 tabview 获取对应的 targetview.Left /Right
通过 ValueAnimator 进行动画位移量计算
通过 ViewCompat 进行SlidingTabIndicator的位移操作

你可能感兴趣的:(TabLayout(二))