最近google在supportDesign中添加了android.support.design.widget.TabItem,因为documentation说:
TabItem is a special ‘view’ which allows you to declare tab items for
a TabLayout within a layout. This view is not actually added to
TabLayout,it is just a dummy which allows setting of a tab items’s
text,icon and custom layout.
但是当我将TabItems添加到我的TabLayout时:
没有显示任何内容(事实上Tabs的位置存在,但Icon / Text不存在).有谁知道如何通过xml使用TabItem?
最佳答案
基于this答案,TabItem与tabLayout.setupViewPager有冲突,图标消失.为了使它工作,你应该实现两个方法,如下面的,并避免使用setupViewPager方法:
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
pager.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position,float positionOffset,int positionOffsetPixels) {
}
@Override
public void onPageSelected(int position) {
tabLayout.getTabAt(position).select();
}
@Override
public void onPageScrollStateChanged(int state) {
}
});