大部分APP的主界面都很类似,要么底部导航的,要么就是侧滑菜单,还有底部导航+侧滑菜单的;底部导航实现大概有几种方式:
- TabHost+Fragment
- RadioGroup+Fragment
- FragmentTabHost+Fragment
- BottomNavigator+Fragment
今天说一下FragmentTabHost+Fragment实现方式(布局文件在最下面):
1、定义一个TabBean,存放每个tab;
public class TabBean {
private int title; // 文字
private int icon; // 图标
private Class fragment; // 对应fragment
public TabBean(Class fragment, int title, int icon ) {
this.title = title;
this.icon = icon;
this.fragment = fragment;
}
public int getTitle() {
return title;
}
public void setTitle(int title) {
this.title = title;
}
public int getIcon() {
return icon;
}
public void setIcon(int icon) {
this.icon = icon;
}
public Class getFragment() {
return fragment;
}
public void setFragment(Class fragment) {
this.fragment = fragment;
}
}
2、初始化数据集合
private List<TabBean> mTabs = null;
private void initData() {
mTabs = new ArrayList();
mTabs.add(new TabBean(HomeFragment.class,R.string.home,R.mipmap.ic_launcher_round));
mTabs.add(new TabBean(HotFragment.class,R.string.hot,R.mipmap.ic_launcher_round));
mTabs.add(new TabBean(CategoryFragment.class,R.string.category,R.mipmap.ic_launcher_round));
mTabs.add(new TabBean(CartFragment.class,R.string.cart,R.mipmap.ic_launcher_round));
mTabs.add(new TabBean(MineFragment.class,R.string.mine,R.mipmap.ic_launcher_round));
}
3、实例化控件,设置数据;
private void initView() {
mTabHost.setup(this,getSupportFragmentManager(),R.id.flContent);
for(TabBean tab : mTabs){
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(getString(tab.getTitle()));
tabSpec.setIndicator(buildIndicator(tab));
tabSpec.setContent(new TabHost.TabContentFactory() {
@Override
public View createTabContent(String s) {
return new View(MainActivity.this);
}
});
mTabHost.addTab(tabSpec,tab.getFragment(),null);
}
mTabHost.setCurrentTab(0); //默认选中
mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String tabTitle) {
//修改选中Tab的样式(类似状态选择器)
TabWidget tabw = mTabHost.getTabWidget();
for (int i = 0; i < tabw.getChildCount(); i++) {
View view = tabw.getChildAt(i);
TextView tv = (TextView) view.findViewById(R.id.tab_title);
if (i == mTabHost.getCurrentTab()) {
tv.setTextColor(getResources().getColor(R.color.colorAccent));
} else {
tv.setTextColor(getResources().getColor(R.color.textColor));
}
}
}
});
}
//构建Indicator
private View buildIndicator(TabBean tab){
View view = LayoutInflater.from(this).inflate(R.layout.tab_indicator,null);
ImageView img = (ImageView) view.findViewById(R.id.tab_icon);
TextView text = (TextView) view.findViewById(R.id.tab_title);
img.setBackgroundResource(tab.getIcon());
text.setText(tab.getTitle());
return view;
}
完成了!!! 下面是布局文件(界面的和tab的)
<?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=".MainActivity">
<FrameLayout
android:id="@+id/flContent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"></FrameLayout>
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabHost"
android:layout_width="match_parent"
android:layout_height="48dp">
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:descendantFocusability="afterDescendants"
android:paddingBottom="4dp"
android:paddingTop="6dp">
<ImageView
android:id="@+id/tab_icon"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_centerHorizontal="true"
tools:ignore="ContentDescription" />
<TextView
android:id="@+id/tab_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tab_icon"
android:layout_centerHorizontal="true"
android:clickable="false"
android:enabled="false"
android:focusable="false"
android:gravity="center"
android:textColor="@color/textColor"
android:textSize="12sp"
tools:text="tab" />
</RelativeLayout>
FragmentTabHost这个控件每次切换Fragment,都会走Fragment的onCreateView和onDestroyView方法,多以每次切换都会创建和销毁Fragment实例,下篇文章说一下自定义FragmentTabHost避免重复创建和销毁Fragment实例,让Fragment隐藏hide和显示show;FragmentTabHost切换Fragment时保存状态,避免切换Fragment走onCreateView和onDestroyView方法;