使用Fragment+FragmentManage实现tab主界面(不可滑动)
优点:
- 简单、易懂
- 代码整洁
注意:Fragment的hide和show不走任何的生命周期,hide和show的时候走
@Override
public void onHiddenChanged(boolean hidden) {
super.onHiddenChanged(hidden);
if (hidden){//相当于调用了onPause();
}else {//类似于调用了onResume(与onResume区别:第一次创建Fragment的时候没走这个方法)
}
}
效果图:
![](https://img.haomeiwen.com/i7577467/8cbcfc4d4de24170.gif)
@Route(path = "/main/main")
public class MainActivity extends BaseActivity {
@BindView(R2.id.fl)
FrameLayout fl;
@BindView(R2.id.ll_bottom)
LinearLayout llBottom;
private Fragment mCurrentFragment;
private List<Fragment> fragmentList = new ArrayList<>();
@Override
protected int getLayoutResID() {
return R.layout.main_activity_main;
}
@Override
protected void init(Bundle savedInstanceState) {
for (int i = 0; i < llBottom.getChildCount(); i++) {
//fragmentList初始化
fragmentList.add(null);
final int finalI = i;
//顺便设置子View的点击事件
llBottom.getChildAt(i).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setTab(finalI);
}
});
}
/**
* 要写在fragmentList初始化之后
*/
setTab(0);
}
@Override
protected void initEvent() {
}
private void setTab(int pos) {
Fragment fragment = getFragment(pos);
if (fragment != null && mCurrentFragment != fragment) {
Fragment oldFragment = mCurrentFragment;
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
if (oldFragment != null) {
transaction.hide(oldFragment);
}
if (fragment.isAdded()) {
transaction.show(fragment);
} else {
transaction.add(R.id.fl, fragment);
}
transaction.commit();
mCurrentFragment = fragment;
}
}
public Fragment getFragment(int pos) {
//使用fragmentList存储Fragment,不至于产生多个成员变量的fragment影响代码整洁度
if (fragmentList.get(pos) != null) {
return fragmentList.get(pos);
}
Fragment fragment = null;
//根据tab数量自行加减fragment
switch (pos) {
case 0:
fragment = new FirstFragment();
break;
case 1:
fragment = (Fragment) ARouter.getInstance().build(ARouterConstants.FRAGMENT_USER_USER).navigation();
break;
case 2:
fragment = (Fragment) ARouter.getInstance().build(ARouterConstants.FRAGMENT_DESIGN_DESIGN).navigation();
break;
}
fragmentList.set(pos, fragment);
return fragment;
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="hjt.com.module.main.activity.MainActivity">
<FrameLayout
android:id="@+id/fl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<LinearLayout
android:id="@+id/ll_bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#2a7e2a"
android:gravity="center"
android:padding="10dp"
android:text="module_main"
android:textColor="@color/white"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#1cbaad"
android:gravity="center"
android:padding="10dp"
android:text="module_user"
android:textColor="@color/white"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#831f81"
android:gravity="center"
android:maxLines="1"
android:padding="10dp"
android:text="module_design"
android:textColor="@color/white"/>
</LinearLayout>
</LinearLayout>
自己新建几个Fragment添加进去就OK了
网友评论