实现Tab功能
网上实现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功能的更多相关文章
- 手动实现jQuery Tools里面tab功能
平时开发中用的Javascript类库都是jQuery,用到插件或者第三方类库能从jQuery Tools里面找到,基本不用其他的.当然有时同事喜欢使用jQuery UI里面的插件.并且jQuery ...
- jquery 回车切换 tab功能
挺有趣的,Jquery 回车切换tab功能的实现哦 <html> <head><!--jquery库.js--></head> <body> ...
- Android UI ActionBar功能-自定义Tab功能
还可以使用ActionBar实现Tab选项卡功能: 官方帮助文档:http://wear.techbrood.com/training/basics/actionbar/styling.html#Cu ...
- ActivityGroup实现tab功能
android.app包中含有一个ActivityGroup类,该类是Activity的容器,可以包含多个嵌套进来的 Activitys,这篇文章就是借助ActivityGroup可以嵌套Activi ...
- c# winfrom 页面的enter变为tab 功能使用 在特定的按钮里面如何继续当enter使用求大神帮忙解答一下 !!急
enter 当tab 键用 已经实现 :例如按回车的时候切换一直走 ,走到一个按钮如何让回车键在这个按钮的时候还是执行enter按钮的功能而不是tab 求大神解答一下, 目前页面tab功能改为 ...
- 自己在项目中写的一个Jquery插件和Jquery tab 功能
后台查询结果 PDFSearchResult实体类: [DataContract(Name = "PDFSearchResult")] public class PDFSearch ...
- easyui实现树形菜单Tab功能、layout布局
一:常见三种前端ui框架 在初学者入门的状态下,我们常见的前端框架有三种且都有自己的官方网站: 1.easyui:官方网站(http://www.jeasyui.net/) 基于jquery的用户页面 ...
- 大熊君JavaScript插件化开发------(实战篇之DXJ UI ------ Tab功能扩展完结版)
一,开篇分析 Hi,大家好!大熊君又和大家见面了,还记得上一篇文章吗.主要讲述了一个“Tab”插件是如何组织代码以及实现的”,以及过程化设计与面向对象思想设计相结合的方式是 如何设计一个插件的,两种方 ...
- jQuery+css实现tab功能
点击我我会消失 Click me 点击按钮我会消失,再点击我会出现 演示tab tab1 tab2 tab3 [环球时报记者 郭芳] “中国秘密发射新快速响应火箭”,25日,在中国官方媒体报道我国“快 ...
随机推荐
- Codeforces Beta Round #4 (Div. 2 Only) A. Watermelon【暴力/数学/只有偶数才能分解为两个偶数】
time limit per test 1 second memory limit per test 64 megabytes input standard input output standard ...
- [Python Cookbook] Pandas: 3 Ways to define a DataFrame
Using Series (Row-Wise) import pandas as pd purchase_1 = pd.Series({'Name': 'Chris', 'Item Purchased ...
- codevs——1230 元素查找
时间限制: 1 s 空间限制: 128000 Ks 题目等级 : 钻石 Diamond 题解 查看运行结果 题目描述 Description 给出n个正整数,然后有m个询问,每个询问一个整数 ...
- [POI2014]Ant colony
题目大意: 给定一棵$n(n\le10^6)$个结点的树.在每个叶子结点,有$g$群蚂蚁要从外面进来,其中第$i$群有$m_i$只蚂蚁.这些蚂蚁依次爬树(一群蚂蚁爬完后才会爬另一群),若当前经过结点度 ...
- String&&StringBuilder&&StringBuffer
在java中提供三个操作字符串的类:String,StringBuilder,StringBuffer (1)什么是字符串:多个字符的集合 (2)String 是内容不可变的字符串.(底层使用了一个不 ...
- OC语言基础之函数与方法
方法 1.对象方法都是以减号 - 2.对象方法的声明必须写在@interface和@end之间 对象方法的实现必须写在@implementation和@end之间 3.对象方法只能由对象来调用 ...
- Linux的五个查找命令:find,locate,whereis,which,type 及其区别
1. find find是最常见和最强大的查找命令,你可以用它找到任何你想找的文件. find的使用格式如下: $ find <指定目录> <指定条件> <指定动作> ...
- asp.net mvc 生成二维码
生成二维码,帮助类: using Gma.QrCodeNet.Encoding; using Gma.QrCodeNet.Encoding.Windows.Render; using System; ...
- Centos7.x系统优化
1.安装常用软件 yum install tree nmap sysstat lrzsz dos2unix wget net-tools ntpdate -y 2.配置yum源 mv /etc/yu ...
- nginx资源争夺问题
nginx资源争夺问题 多个配置之间存在资源争夺的情况,需要进行整理: 学习了:https://blog.csdn.net/veryisjava/article/details/72917894 ng ...