tabLayout结合viewPager使用时tabLayout不显示问题解决方案
一般tabLayout结合viewPager使用的代码都是这样写的
public class MainActivity extends AppCompatActivity {
private static final String TAG ="MainActivity" ;
List<MyFragment> fragments;
ViewPager pager;
TabLayout mytab;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
pager=findViewById (R.id.view_pager);
mytab=findViewById (R.id.tab);
fragments=new ArrayList<> ();
for(int i=0;i<6;i++){
fragments.add (new MyFragment ());
}
MyFragmentPagerAdapter adapter=new MyFragmentPagerAdapter (getSupportFragmentManager (),fragments);
pager.setAdapter (adapter);
mytab.addTab(mytab.newTab().setText("选项卡一").setIcon(null));
mytab.addTab(mytab.newTab().setText("选项卡二").setIcon(null));
mytab.addTab(mytab.newTab().setText("选项卡三").setIcon(null));
mytab.addTab(mytab.newTab().setText("选项卡四").setIcon(null));
mytab.addTab(mytab.newTab().setText("选项卡五").setIcon(null));
mytab.addTab(mytab.newTab().setText("选项卡六").setIcon(null));
mytab.setupWithViewPager (pager);
}
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public List<MyFragment> mFragmentList;
public FragmentManager mFragmentManager;
public MyFragmentPagerAdapter(FragmentManager fm, List<MyFragment> fragmentList) {
super(fm);
mFragmentManager=fm;
mFragmentList=fragmentList;
}
@Override
public Fragment getItem(int position) {
return mFragmentList==null?null:mFragmentList.get(position);
}
@Override
public int getCount() {
return mFragmentList==null?0:mFragmentList.size();
}
@Override
public Object instantiateItem(ViewGroup container, int position) {
Fragment fragment = (Fragment)super.instantiateItem(container,position);
mFragmentManager.beginTransaction().show(fragment).commit();
return fragment;
}
@Override
public void destroyItem(ViewGroup container, int position, Object object) {
Fragment fragment = mFragmentList.get(position);
mFragmentManager.beginTransaction().hide(fragment).commit();
//super.destroyItem (container,position ,object );
}
}
}
运行起来却是这样的
tab的标题不见了?但是拿掉mytab.setupWithViewPager (pager);
之后又可以用了,只是tabLayout和viewPager失联了。看来这个方法我们还是没有掌握它使用方法。既然这样我们就手写一个适配吧,毕竟自己写的viewPager和tabLayout还是自己更了解码嘛!
其实就是实现两者的同步呗,那就在tabLayout被选中时候跳转到viewPager相应的页面,在viewPager被选中时候跳转到tabLayout相应的页面就好了。
下面就开始吧
一、tabLayout被选中时候跳转到viewPager相应的页面
mytab.addOnTabSelectedListener (new TabLayout.OnTabSelectedListener () {
@Override
public void onTabSelected(TabLayout.Tab tab) {
Log.d (TAG, "onTabSelected: "+tab.getPosition ());
pager.setCurrentItem (tab.getPosition ());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
二、viewPager被选中时候跳转到tabLayout相应的页面
pager.setOnPageChangeListener (new ViewPager.OnPageChangeListener () {
@Override
public void onPageScrolled(int i, float v, int i1) {
}
@Override
public void onPageSelected(int i) {
mytab.setScrollPosition (pager.getCurrentItem (), 0, true);
}
@Override
public void onPageScrollStateChanged(int i) {
}
});
就是为viewPager和tabLayout添加监听器然后跳转就OK了。再运行就可以了