实现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日,在中国官方媒体报道我国“快 ...
随机推荐
- MYSQL的longtext字段能放多少数据?
生产上遇到问题, 同事说MYSQL里的字段放不下5m大小的数据. 于是,将django model里textfield里的max_length变长了. 依然无效, 于是,更改mysql的设置: set ...
- 树上各种DFS姿势算法笔记
树是指由n个点,n-1条边构成的联通无向图.如果有一棵树,它的每一条边(u,v)都有一个权值l(u,v),我们把这样的树称作带权树. 我们知道对于树上的任意两个点,他们之间的路径是唯一的.对于两个点u ...
- HashSet底层存储元素的源码分析
此类实现 Set 接口,由哈希表(实际上是一个 HashMap 实例)支持.它不保证 set 的迭代顺序:特别是它不保证该顺序恒久不变.此类允许使用 null 元素. HashSet<Strin ...
- 洛谷——2722总分 Score Inflation
题目背景 学生在我们USACO的竞赛中的得分越多我们越高兴. 我们试着设计我们的竞赛以便人们能尽可能的多得分,这需要你的帮助 题目描述 我们可以从几个种类中选取竞赛的题目,这里的一个"种类& ...
- IO 最快的read 和 write
import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; impo ...
- JNuit
s1:@Test s2:根据提示导包 s3:选中方法名,右击run as -->JUnit Test package songyan.jdbc.learn1; import org.junit. ...
- RTM CU CTP
.RTM: Released To Manufacturing,也就是新产品的正式发布版本,比如SQL 2008 RTM.其后的补丁都是基于这个之上的. .CU: Cumulative Update, ...
- [Bug] 未找到导入的项目“C:\Program Files (x86)\MSBuild\Microsoft\VisualStudio\v11.0\WebApplications\Microsoft.WebApplication.targets”
This is very easy to do. Open your build definition and go to the "Process" page. Then und ...
- python之生成excel
#_*_coding:utf-8_*_ import MySQLdb import xlwt from datetime import datetime def get_data(sql): # 创建 ...
- bash: /bin/bash^M: bad interpreter: No such file or directory
在windows下编写shell脚本在linux下运行会出报错: [hadoop@master data]$ ./load_ods_table.sh -bash: ./load_ods_table.s ...