用到
com.google.android.material.tabs.TabLayout
androidx.viewpager.widget.ViewPager
FragmentPagerAdapter
問題1蚀同,當數(shù)據(jù)源變化時去切換缅刽,未重置頁面啊掏。
解決:
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
Fragment fragment = (Fragment)viewPager.getAdapter().instantiateItem(viewPager, position);
fragment.refresh();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});
問題2,包含兩個tab頁衰猛,兩個Tab title樣式不一樣
解決:
TabLayout.Tab tab = tabs.getTabAt(0);
View view = tab.getCustomView();
if (null == view) {
tab.setCustomView(R.layout.trans_cus_tab_layout);
((TextView)tab.getCustomView().findViewWithTag("tabtitle")).setText(sectionsPagerAdapter.getPageTitle(0));
}
TabLayout.Tab tab2 = tabs.getTabAt(1);
View view2 = tab2.getCustomView();
if (null == view2) {
tab2.setCustomView(R.layout.trans_cus_tab_layout2);
((TextView)tab2.getCustomView().findViewWithTag("tabtitle")).setText(sectionsPagerAdapter.getPageTitle(1));
}