大部分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方法;

Android控件使用FragmentTabHost,切换Fragment;的更多相关文章

  1. Android控件Gridview实现仿支付宝首页,Fragment底部按钮切换和登录圆形头像

    此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...

  2. Android控件Gridview实现多个menu模块,可添加可删除

    此案例主要讲的是Android控件Gridview(九宫格)完美实现仿支付宝首页,包含添加和删除功能:Fragment底部按钮切换的效果,包含四个模块,登录页面圆形头像等,一个小项目的初始布局. 效果 ...

  3. 让多个Fragment 切换时不重新实例化、FragmentTabHost切换Fragment时避免UI重新加载

    http://www.tuicool.com/articles/FJ7VBb FragmentTabHost切换Fragment时避免UI重新加载 不过,初次实现时发现有个缺陷,每次FragmentT ...

  4. android控件的属性

    android控件的属性 本节描述android空间的位置,内容等相关属性及属性的含义 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ( ...

  5. Android控件之ImageSwticher

    Android控件之ImageSwticher 1. ImageSwticher介绍 ImageSwitcher是图片切换的控件,它能实现图片切换时的动画效果,包括图片导入效果.图片消失效果等等.An ...

  6. [Android Pro] android控件ListView顶部或者底部也显示分割线

    reference to  :  http://blog.csdn.net/lovexieyuan520/article/details/50846569 在默认的Android控件ListView在 ...

  7. Android 控件架构及View、ViewGroup的测量

    附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...

  8. Android - 控件android:ems属性

    Android - 控件android:ems属性http://blog.csdn.net/caroline_wendy/article/details/41684255?utm_source=tui ...

  9. Android 控件知识点,

    一.Android控件具有visibility属性,可以取三个值:visible(默认值)可见,invisible(不可见,但仍然占据原有的位置和大小,可以看做是变得透明了),gone(空间不仅不可见 ...

随机推荐

  1. Vim 安装、配置及复制粘贴操作

    1.安装:sudo apt-get install vim 2.配置:cd ~ #进入用户主目录 touch .vimrc #.后缀文件不可见 vi .vimrc #打开文件 输入: set cind ...

  2. linux软件管理 软件安装

    软件包分类 1) 源代码包   脚本安装包 2) 二进制包   (RPM包,系统默认包) 源码包编译后形成二进制包 JDK的安装 下载jdk的文件解压 tar -zxvf jdk-8u144-linu ...

  3. Java程序员从阿里拿到offer回来,这些面试题你会吗?

    前不久刚从阿里面试回来,为了这场面试可以说准备了一个半月,做的准备就是刷题和看视频看书充实自己的技术,话说是真难啊,不过还算顺利拿到了offer,有很多面试题我已经记不起来了,这些是当天回家整理好的, ...

  4. 第一份offer

    11月6日参加的面试,今天签完三方,回头想想,感慨万千. (很多过程没有详细写,只保留了基本的客观事实,避免自吹嫌疑.....) 6号面试,当时来了能有100-200人,以川大和电子科大研究生为主,主 ...

  5. 用Python自动发送邮件

    用Python自动发送邮件     最近需要在服务器上处理一些耗时比较长的任务,因此想到利用python写一个自动发送邮件的脚本,在任务执行完毕后发送邮件通知我.以下代码以163邮箱为例: 开通163 ...

  6. CSS图形——实现圆角

    css实现圆角 css2.1给元素添加圆角是一件很麻烦的事,老办法是用背景图片实现,制作比较麻烦.css3,border-radius的属性,使圆角属性得到完美的解决. 语法 border-radiu ...

  7. 健壮程序之--SQL优化

    (仅为自己以后快速参考!!!) (1)防止数据库字段为空 解决方案(1):DECODE() DECODE(JN.USERID, , '待聘', UI.USERNAME) USERNAME, 解决方案( ...

  8. Java学习笔记43(Spring的jdbc模板)

    在之前的学习中,我们执行sql语句,需要频繁的开流,关流比较麻烦,为了更加的简化代码,我们使用Spring 的jdbc模板jdbcTemplate来简化我们的代码量:需要导入的包有: 我们在之前的dr ...

  9. c++ 网络库

    1.libevent 2.boost::asio 3.ace boost::asio以前看过,不过忘记了 学习,学习

  10. 国内优秀MVC开源框架jfinal简介

    JFinal简介 JFinal 项目开发始于2011年初,作者詹波(James Zhan)曾任搜格信息技术有限公司Java架构师,北京信息管理科学研究所CTO,现任微格网际(北京)科技有限公司联合创始 ...