package com.bsk.zhangbo.demoforbsk.ui.activity; import android.support.design.widget.TabLayout; import android.support.v4.view.ViewPager; import android.os.Bundle; import com.bsk.zhangbo.demoforbsk.R; import com.bsk.zhangbo.demoforbsk.adapter.MyFragmentPagerAdapter; import com.bsk.zhangbo.demoforbsk.base.BaseActivity; import com.bsk.zhangbo.demoforbsk.ui.fragment.AttendanceManagerFragment; import com.bsk.zhangbo.demoforbsk.ui.fragment.DeviceManagerFragment; import com.bsk.zhangbo.demoforbsk.ui.fragment.OneFragment; import com.bsk.zhangbo.demoforbsk.ui.fragment.RegisterFragment; import com.bsk.zhangbo.demoforbsk.ui.fragment.TelPhoneFragment; import com.bsk.zhangbo.demoforbsk.ui.fragment.VisitorManagerFragment; import com.bsk.zhangbo.demoforbsk.ui.fragment.VisitorRegisterFragment; import com.bsk.zhangbo.demoforbsk.ui.fragment.TwoFragment; import com.bsk.zhangbo.demoforbsk.util.TabNameUtil; import java.util.Arrays; import java.util.List; public class MainActivity extends BaseActivity { private TabLayout mTabLayout; private ViewPager mViewPager; @Override public int getLayoutId() { return R.layout.activity_main; } @Override public void initViews(Bundle savedInstanceState) { mTabLayout = (TabLayout) findViewById(R.id.tab_layout); mViewPager = (ViewPager) findViewById(R.id.viewpager); mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL); } @Override public void initToolBar() { } @Override public void initListeners() { } @Override public void initData() { List tabNamesList = Arrays.asList(TabNameUtil.tabNames); int[] tabIcons = TabNameUtil.tabIcons; MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(),MainActivity.this); myFragmentPagerAdapter.addFragment(OneFragment.newInstance(), tabNamesList.get(0),tabIcons[0]); myFragmentPagerAdapter.addFragment(RegisterFragment.newInstance(), tabNamesList.get(1),tabIcons[1]); myFragmentPagerAdapter.addFragment(VisitorRegisterFragment.newInstance(), tabNamesList.get(2),tabIcons[2]); myFragmentPagerAdapter.addFragment(TelPhoneFragment.newInstance(), tabNamesList.get(3),tabIcons[3]); myFragmentPagerAdapter.addFragment(VisitorManagerFragment.newInstance(), tabNamesList.get(4),tabIcons[4]); myFragmentPagerAdapter.addFragment(AttendanceManagerFragment.newInstance(), tabNamesList.get(5),tabIcons[5]); myFragmentPagerAdapter.addFragment(DeviceManagerFragment.newInstance(), tabNamesList.get(6),tabIcons[6]); mViewPager.setAdapter(myFragmentPagerAdapter); mViewPager.setOffscreenPageLimit(6);//viewpager缓存个数 mTabLayout.setupWithViewPager(mViewPager); mTabLayout.setTabMode(TabLayout.MODE_FIXED); for (int i = 0; i < mTabLayout.getTabCount(); i++) { TabLayout.Tab tab = mTabLayout.getTabAt(i); tab.setCustomView(myFragmentPagerAdapter.getTabView(i)); } } }