當(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的位移操作