TabLayout(二)

當(dāng)進(jìn)行 tablayout 切換時(shí)的處理流程
在 TabLayout.setupWithViewPager(vp)過(guò)程中
我們通過(guò) TabLayout.pageChangeListener 獲取 viewpager 的翻頁(yè)事件獲取頁(yè)碼 position
對(duì)當(dāng)前 TabLayout 添加一個(gè)為 ViewPager 準(zhǔn)備的 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);
        }

我們正常對(duì) tablayout 的切換操作是通過(guò)點(diǎn)擊對(duì)應(yīng) tab 來(lái)進(jìn)行,所以來(lái)看 tabview 的點(diǎn)擊監(jiān)聽(tīng)

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

                this.tab.select();
                return true;
            } else {
                return handled;
            }
        }
//進(jìn)入到 tab.select();
        public void select() {
            if (this.parent == null) {
                throw new IllegalArgumentException("Tab not attached to a TabLayout");
            } else {
                this.parent.selectTab(this);
            }
        }
//然后進(jìn)入到 TabLayout.selectTab
    void selectTab(TabLayout.Tab tab, boolean updateIndicator) {
        TabLayout.Tab currentTab = this.selectedTab;
        if (currentTab == tab) {//與上一次選中的 tab相同
            if (currentTab != null) {
//遍歷所有的 onTabSelectedListener 監(jiān)聽(tīng)并進(jìn)行回調(diào)
                this.dispatchTabReselected(tab);
//執(zhí)行動(dòng)畫(huà)
                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);
            }
        }

    }

根據(jù)選中的 tabview 獲取對(duì)應(yīng)的 targetview.Left /Right
通過(guò) ValueAnimator 進(jìn)行動(dòng)畫(huà)位移量計(jì)算
通過(guò) ViewCompat 進(jìn)行SlidingTabIndicator的位移操作

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀(guān)點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 開(kāi)源項(xiàng)目效果 調(diào)用實(shí)例 必練基本功 Android studio 項(xiàng)目導(dǎo)入依賴(lài)compile路徑 dependen...
    隨心隨性_0a25閱讀 2,509評(píng)論 1 0
  • 一、簡(jiǎn)述 TabLayout是Android Support Design庫(kù)的新控件,可以用來(lái)實(shí)現(xiàn)開(kāi)源框架View...
    CQ_TYL閱讀 1,802評(píng)論 0 5
  • 一、概述 它也是design中新出的一個(gè)控件,用來(lái)實(shí)現(xiàn)選項(xiàng)卡切換的效果,以前我們常用RadioGroup+Radi...
    Serenity那年閱讀 2,558評(píng)論 0 9
  • 文章目錄 文章修修補(bǔ)補(bǔ)添加了不少,主要分3個(gè)時(shí)間段 最早實(shí)現(xiàn),就是在tab切換的時(shí)候在下邊畫(huà)一條線(xiàn) 不畫(huà)線(xiàn)了,因?yàn)?..
    有點(diǎn)健忘閱讀 1,979評(píng)論 0 0
  • 我小時(shí)候在農(nóng)村長(zhǎng)大,我家在陜西省黃土高坡上的一個(gè)小山村,太峪溝。 這個(gè)村里出了一個(gè)英雄,就是智取華山的劉吉堯劉...
    蜂人蜜語(yǔ)閱讀 969評(píng)論 10 18

友情鏈接更多精彩內(nèi)容