网上实现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. android的百度地图开发(二) 定位

    参考:http://blog.csdn.net/mr_wzc/article/details/51590485 第一步,初始化LocationClient类 //获取地图控件引用 mMapView = ...

  2. Appium+python自动化4-元素定位uiautomatorviewer【转载】

    前言 环境搭建好了,下一步元素定位,元素定位本篇主要介绍如何使用uiautomatorviewer,通过定位到页面上的元素,然后进行相应的点击等操作. uiautomatorviewer是androi ...

  3. java使用BeanUtils封装file类型表单数据到一个对象中

    package com.cc.web.servlet; import java.io.FileOutputStream; import java.io.IOException; import java ...

  4. Error:scalac: Error: org.jetbrains.jps.incremental.scala.remote.ServerException

    Error:scalac: Error: org.jetbrains.jps.incremental.scala.remote.ServerException reason:JDK与Scala的版本不 ...

  5. servlet多线程同步问题

    Servlet/JSP技术和ASP.PHP等相比,由于其多线程运行而具有很高的执行效率.•由于Servlet/JSP默认是以多线程模式执行的,所以,在编写代码时需要非常细致地考虑多线程的同步问题.•如 ...

  6. Cookie和Session在Node.JS中的实践(二)

    Cookie和Session在Node.JS中的实践(二) cookie篇在作者的上一篇文章Cookie和Session在Node.JS中的实践(一)已经是写得算是比较详细了,有兴趣可以翻看,这篇是s ...

  7. Closest Binary Search Tree Value -- LeetCode

    Given a non-empty binary search tree and a target value, find the value in the BST that is closest t ...

  8. 事务没有提交导致 锁等待Lock wait timeout exceeded异常

    异常:Lock wait timeout exceeded; try restarting transaction 解决办法: 执行select * from information_schema.i ...

  9. CentOS 6与CentOS 7的区别收集

    说明: 1.CentOS与Ubuntu没有什么可比性,底层都是Linux,并且Ubuntu在YY广泛的使用,这些并不能说明那个强大哪个不行,只要能解决问题的都是好家伙. 2.市面上教程基本都是基于6, ...

  10. tiny4412 串口驱动分析一 --- u-boot中的串口驱动

    作者:彭东林 邮箱:pengdonglin137@163.com 开发板:tiny4412ADK+S700 4GB Flash 主机:Wind7 64位 虚拟机:Vmware+Ubuntu12_04 ...