网上实现Tab功能的方法有很多,这里我使用Fragment的方法,我觉着比较简单易懂

MainActivity

     private android.app.FragmentManager fragmentManager;

     private FragmentOne fragmentOne;
private FragmentTwo fragmentTwo;
private FragmentThree fragmentThree;
private FragmentFour fragmentFour; private RelativeLayout layoutOne, layoutTwo, layoutThree, layoutFour; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContent(R.layout.activity_main);
initViews();
fragmentManager = getFragmentManager();
setTabSelection(0);
} private void initViews(){
layoutOne = (RelativeLayout) findViewById(R.id.layoutOne);
layoutOne.setOnClickListener(this);
//init所有的layout,并设置监听器
} @Override
public void onClick(View v){
switch(v.getId()){
case R.id.layoutOne:
setTabSelection(0);
//case 其余所有tab
default:break;
}
} /**
* 根据传入index参数来设置选中的tab页
* @param index
*
*/
private void setTabSelection(int index){
clearSelection();
//开启fragment事物
FragmentTransaction transaction = fragmentManager.beginTransaction();
hideFragments(transaction);
switch (index){
//FragmentOne
case 0:
if(fragmentMain == null){
fragmentOne = new FragmentOne();
transaction.add(R.id.content, fragmentOne);
}else {
transaction.show(fragmentOne);
}
clearSelection();
//set text color and image resource
break;
//case 其余所有
}
transaction.commit();
} /**
* 清除所有选中状态
*
*/
private void clearSelection(){
//set text color and image resource to default
} /**
* 将所有fragment设置为隐藏状态
* @param transaction
* 用于对Fragment执行操作的事务
*/
private void hideFragments(FragmentTransaction transaction){
if(fragmentOne != null){
transaction.hide(fragmentOne);
}
if(fragmentTwo != null){
transaction.hide(fragmentTwo);
}
if(fragmentThree != null){
transaction.hide(fragmentThree);
}
if(fragmentFour != null){
transaction.hide(fragmentFour);
}
}

activity_main.xml  注意:这里bottom为最下边的四个tab的布局,里边有上面代码中的layoutOne,layoutTwo,layoutThree,layoutFour

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"> <View
android:layout_width="match_parent"
android:layout_height="@dimen/public_line_height"
android:background="@color/background"/>
<include
android:id="@+id/layout_bottom"
layout="@layout/bottom"
android:layout_width="fill_parent"
android:layout_height="@dimen/bottom_height"
android:layout_alignParentBottom="true"/> <FrameLayout
android:id="@+id/content"
android:layout_above="@id/layout_bottom"
android:layout_alignBottom="@id/layout_bottom"
android:background="@color/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="@dimen/bottom_height"/>
</RelativeLayout> </LinearLayout>

四个Fragment直接继承Fragment就好

需要传递activity时

     private Context context;
private View view; @Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainLayout = inflater.inflate(R.layout.fragment_main, container, false);
context = this.getActivity();
view = mainLayout;
initView();
return view;
}

这样就可以通过context来进行操作了

实现Tab功能的更多相关文章

  1. 手动实现jQuery Tools里面tab功能

    平时开发中用的Javascript类库都是jQuery,用到插件或者第三方类库能从jQuery Tools里面找到,基本不用其他的.当然有时同事喜欢使用jQuery UI里面的插件.并且jQuery ...

  2. jquery 回车切换 tab功能

    挺有趣的,Jquery 回车切换tab功能的实现哦 <html> <head><!--jquery库.js--></head> <body> ...

  3. Android UI ActionBar功能-自定义Tab功能

    还可以使用ActionBar实现Tab选项卡功能: 官方帮助文档:http://wear.techbrood.com/training/basics/actionbar/styling.html#Cu ...

  4. ActivityGroup实现tab功能

    android.app包中含有一个ActivityGroup类,该类是Activity的容器,可以包含多个嵌套进来的 Activitys,这篇文章就是借助ActivityGroup可以嵌套Activi ...

  5. c# winfrom 页面的enter变为tab 功能使用 在特定的按钮里面如何继续当enter使用求大神帮忙解答一下 !!急

    enter 当tab  键用 已经实现  :例如按回车的时候切换一直走 ,走到一个按钮如何让回车键在这个按钮的时候还是执行enter按钮的功能而不是tab   求大神解答一下, 目前页面tab功能改为 ...

  6. 自己在项目中写的一个Jquery插件和Jquery tab 功能

    后台查询结果 PDFSearchResult实体类: [DataContract(Name = "PDFSearchResult")] public class PDFSearch ...

  7. easyui实现树形菜单Tab功能、layout布局

    一:常见三种前端ui框架 在初学者入门的状态下,我们常见的前端框架有三种且都有自己的官方网站: 1.easyui:官方网站(http://www.jeasyui.net/) 基于jquery的用户页面 ...

  8. 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ Tab功能扩展完结版)

    一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得上一篇文章吗.主要讲述了一个“Tab”插件是如何组织代码以及实现的”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方 ...

  9. jQuery+css实现tab功能

    点击我我会消失 Click me 点击按钮我会消失,再点击我会出现 演示tab tab1 tab2 tab3 [环球时报记者 郭芳] “中国秘密发射新快速响应火箭”,25日,在中国官方媒体报道我国“快 ...

随机推荐

  1. Appium+python自动化3-启动淘宝app【转载】

    前言 前面两篇环境已经搭建好了,接下来就是需要启动APP,如何启动app呢?首先要获取包名,然后获取launcherActivity.获取这两个关键东西的方法很多,这里就不一一多说,小伙伴们可以各显神 ...

  2. poj 3348(凸包面积)

    Cows Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8063   Accepted: 3651 Description ...

  3. 【转】Talend作业设计模式和最佳实践-Part I

    原文地址:https://mp.weixin.qq.com/s?__biz=MzA3OTg1Mzk4Nw==&mid=2453261363&idx=2&sn=e0f426022 ...

  4. C#设计模式学习笔记-单例模式(转)

    C#设计模式学习笔记-单例模式 http://www.cnblogs.com/xun126/archive/2011/03/09/1970807.html 最近在学设计模式,学到创建型模式的时候,碰到 ...

  5. Vue.js之父子组件

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  6. 牛客nowcoder Noip提高组第四场

    https://www.nowcoder.com/acm/contest/175#question A-动态点分治 Description CJK 是一个喜欢数据结构的同学.一天他看到 BZOJ 40 ...

  7. Group Shifted Strings -- LeetCode

    Given a string, we can "shift" each of its letter to its successive letter, for example: & ...

  8. 在sqlserver中如何从字符串中提取数字,英文,中文,过滤重复字符

    原文:在sqlserver中如何从字符串中提取数字,英文,中文,过滤重复字符 最近做项目的时候,经常用到一个字符串中的某些字符,处理起来非常的棘手,现在用下面的方法就可以很方便的从字符串中处理你想要的 ...

  9. 【log4j】springboot项目启动 ,使用的druid数据源,log4j报错 log4j:WARN Please initialize the log4j system properly.

    springboot项目启动 ,使用的druid数据源,log4j报错 -- :: --- [ restartedMain] o.hibernate.annotations.common.Versio ...

  10. ServicePointManager.ServerCertificateValidationCallback 冲突的解决

    ServicePointManager是用于创建. 维护和删除的实例的静态类ServicePoint类. 当应用程序请求对 Internet 资源统一资源标识符 (URI) 的连接通过ServiceP ...