1,使用CommonTabLayout的原因是他既可以和viewpager也可以和framelayout搭配使用,即只指定tab的标签,然后监听切换动态调用对应的方法实现fragment的显示与隐藏。
2,SlidingTabLayout不可用的原因是因为他必须与viewpager结合使用,在设置标签的时候必须设置一个viewpager参数,没有单纯的只设置tab标签的方法,因此不可用。
3,SegmentTabLayout也可以和ViewPager或者framelayout结合使用,但是一般只用于页面顶部的tab效果。
关于CommonTabLayout和SegmentTabLayout具体应该使用哪个视具体的需求而定,例如实现app首页底部多菜单切换,以为一般是要带图标的,所以选择使用CommonTabLayout,可以设置icon。
1,布局文件如下:
<com.flyco.tablayout.CommonTabLayout
android:id="@+id/tabLayout"
tl:tl_indicator_color="@color/red_app"
tl:tl_indicator_height="1.5dp"
tl:tl_textSelectColor="@color/red_app"
tl:tl_textUnselectColor="@color/black_2"
tl:tl_textsize="15sp"
tl:tl_underline_color="@color/list_line"
tl:tl_underline_height="1.5dp"
android:layout_width="match_parent"
android:layout_height="45dp"
android:background="#ffffff" />
</LinearLayout>
<View
android:id="@+id/zhezhao"
android:layout_width="match_parent"
android:layout_height="250dp"
android:visibility="gone"
android:background="#99888888"></View>
</FrameLayout>
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
2,对应的Activity的代码如下:
public class MainActivity2 extends BaseActivity {
@BindView(R.id.tabs)
CommonTabLayout tabs;
private ArrayList<CustomTabEntity> mTabEntities = new ArrayList<>();
private Fragment fg_000, fg_001, fg_002, fg_003;
private String[] mTitles = {"首页", "AI预测", "比赛", "我的"};
private int position = 0;
private FragmentManager mFragmentManager;
public CommonTabLayout getTabs() {
return tabs;
}
@Override
public int setLayout() {
return R.layout.activity_main;
}
@Override
public void initView() {
// 设置透明状态栏
StatusBarUtil.setStatusBarTranslucent(this);
// 创建Fragment管理对象manager
mFragmentManager = getSupportFragmentManager();
// 初始化tab控件
initTab();
}
@Override
protected void initState(Bundle savedInstanceState) {
if (savedInstanceState == null) {
// 为null说明是第一次加载
// 检查版本,有更新弹出对话框
// Beta.checkUpgrade(false, false);
// 默认加载首页
fg_000 = MainIndexFragment.newInstance();
selectFragment(fg_000);
} else {
// 进行了界面重绘
position = savedInstanceState.getInt("position", 0);
fg_000 = mFragmentManager.findFragmentByTag(MainIndexFragment.class.getName());
fg_001 = mFragmentManager.findFragmentByTag(MainYuceFragment2.class.getName());
fg_002 = mFragmentManager.findFragmentByTag(MainTeamFragment.class.getName());
fg_003 = mFragmentManager.findFragmentByTag(MainMeFragment.class.getName());
switch (position){
case 0:
selectFragment(fg_000);
break;
case 1:
selectFragment(fg_001);
break;
case 2:
selectFragment(fg_002);
break;
case 3:
selectFragment(fg_003);
break;
}
}
}
// Activity被异常销毁时调用
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("position", position);
}
// 初始化tab
private void initTab() {
for (String mTitle : mTitles) {
if (mTitles[0].equals(mTitle)) {
mTabEntities.add(new TabEntity(mTitle, R.drawable.main_tabs_index_ok, R.drawable.main_tabs_index_no));
} else if (mTitles[1].equals(mTitle)) {
mTabEntities.add(new TabEntity(mTitle, R.drawable.main_tabs_yuce_ok, R.drawable.main_tabs_yuce_no));
} else if (mTitles[2].equals(mTitle)) {
mTabEntities.add(new TabEntity(mTitle, R.drawable.main_tabs_team_ok, R.drawable.main_tabs_team_no));
} else if (mTitles[3].equals(mTitle)) {
mTabEntities.add(new TabEntity(mTitle, R.drawable.main_tabs_me_ok, R.drawable.main_tabs_me_no));
}
}
tabs.setTabData(mTabEntities);
}
// 显示指定的Fragment
private void selectFragment(Fragment fragment) {
FragmentTransaction transaction = mFragmentManager.beginTransaction();
hideAllFragement(transaction);
if (!fragment.isAdded()) {
transaction.add(R.id.flContent, fragment, fragment.getClass().getName());
}
transaction.show(fragment).commit();
}
// 隐藏所有的Fragment
public void hideAllFragement(FragmentTransaction transaction) {
if (fg_000 != null) {
transaction.hide(fg_000);
}
if (fg_001 != null) {
transaction.hide(fg_001);
}
if (fg_002 != null) {
transaction.hide(fg_002);
}
if (fg_003 != null) {
transaction.hide(fg_003);
}
}
@Override
protected void setListener() {
// 设置tab点击切换监听
tabs.setOnTabSelectListener(new OnTabSelectListener() {
@Override
public void onTabSelect(int posi) {
switch (posi) {
case 0:
if (fg_000 == null) {
fg_000 = MainIndexFragment.newInstance();
}
selectFragment(fg_000);
position = 0;
break;
case 1:
if (fg_001 == null) {
fg_001 = MainYuceFragment2.newInstance();
}
selectFragment(fg_001);
position = 1;
break;
case 2:
if (fg_002 == null) {
fg_002 = MainTeamFragment.newInstance();
}
selectFragment(fg_002);
position = 2;
break;
case 3:
if (fg_003 == null) {
fg_003 = MainMeFragment.newInstance();
}
selectFragment(fg_003);
position = 3;
break;
}
}
@Override
public void onTabReselect(int posi) {
}
});
}
}
其中的 initState 方法是BaseActivity的onCreate中定义的方法,如下:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置BaseActivity的布局
setContentView(setLayout());
//绑定ButterKnife
ButterKnife.bind(this);
// 注册,
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
// 初始化网络
initNet();
// 初始化页面
initView();
// 子类中需要获取Activity异常销毁时的值的时候使用
initState(savedInstanceState);
// 设置数据
initData();
}