Fragment嵌套Fragment实现的Tab导航
要实现的效果就是底部导航,具体到每一个Fragment又在上部设置一个导航条,分成两个Fragment实现。效果图是:

首先给出activity的layout:
<android.support.v4.app.FragmentTabHost xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" /> </LinearLayout> </android.support.v4.app.FragmentTabHost>
然后是main activity的代码:
package com.example.xxx.myapplication; import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Menu;
import android.view.MenuItem; public class MainActivity extends FragmentActivity { private FragmentTabHost mTabHost; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), android.R.id.tabcontent);
mTabHost.addTab(mTabHost.newTabSpec("Tab1").setIndicator("TAB1", null), Fragment1.class, null);
mTabHost.addTab(mTabHost.newTabSpec("Tab2").setIndicator("TAB2", null), Fragment2.class,
null);
}
}
我在Fragment2中嵌套两个fragment,这个layout比较重要,容易出问题的地方就是这里。
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"> <LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="@color/bg_color"
android:textColor="@android:color/white"
android:textSize="24sp"
android:gravity="center_horizontal|center_vertical"
android:text="hello"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"/>
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/> </LinearLayout> </android.support.v4.app.FragmentTabHost>
fragment2的代码实现
public class Fragment2 extends Fragment {
private FragmentTabHost tabHost;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_test, null);
tabHost = (FragmentTabHost) view.findViewById(android.R.id.tabhost);
tabHost.setup(getActivity(),getChildFragmentManager(), android.R.id.tabcontent);
tabHost.addTab(tabHost.newTabSpec("friends").setIndicator("friends", null), FriendsFragment
.class, null);
tabHost.addTab(tabHost.newTabSpec("groups").setIndicator("groups", null), GroupFragment
.class,
null);
tabHost.setCurrentTab(0);
return view;
}
}
Fragment嵌套Fragment实现的Tab导航的更多相关文章
- android fragment嵌套fragment出现的问题:no activity
package com.example.fragmentNavigation2.fragment; import android.content.Context; import android.os. ...
- Android 中关于Fragment嵌套Fragment的问题
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5802146.html 问题描述: 在项目中Activity A中嵌套Fragment B,Fragment ...
- Fragment 嵌套Fragment注意事项
最近项目新功能需要在垂直方方向可以循环滚动,并且水平方向也可以水平循环滚动,并且可以定位到指定item上.很自然的想到了ViewPager和 VerticalViewPager来解决项目需求,UI的大 ...
- Fragment嵌套Fragment时候。子类fragment调用父容器Fragment方法
业务场景:有的时候我们的页面可能是Activity 嵌套多个Fragment ..其中某个Fragment 又嵌套多个Fragment. 其中某个子Fragment 定义为 NewsFragmen ...
- Fragment嵌套Fragment时遇到的那些坑
由于项目要改成MVP模式,自然会用到了Fragment,有时候可能会需要一个Fragment里面嵌套多个Fragment,并且add完成后需要立即刷新子Fragment的View,那么这个时候就会抛出 ...
- 关于Fragment里面嵌套fragment
今天看到一篇好文章 https://www.2cto.com/kf/201609/545979.html 转载过来记录一下,往后需要的时候可以随时查看: 接下来进入正题: 动态fragment的使用 ...
- 浅谈Android Fragment嵌套使用存在的一些BUG以及解决方法
时间 2014-03-18 18:00:55 eoe博客 原文 http://my.eoe.cn/916054/archive/24053.html 主题 安卓开发 自从Android3.0引入了F ...
- Fragment嵌套
当我们从一个Activity启动了一个Fragment,然后在这个Fragment中又去实例化了一些子Fragment,在子Fragment中去有返回的启动了另外一个Activity,即通过start ...
- Fragment(一)--Fragment用法常见问题
fragment notes fragment相关内容包括 基本定义与使用 回退栈内部实现 fragment通信(与activity 与fragment) DialogFragment VP + Fr ...
随机推荐
- JS版汉字与拼音互转终极方案,附简单的JS拼音输入法
原文:http://www.cnblogs.com/liuxianan/p/pinyinjs.html 前言 网上关于JS实现汉字和拼音互转的文章很多,但是比较杂乱,都是互相抄来抄去,而且有的不支持多 ...
- iOS:自定义代码块{ }
1.Xcode本身带有编码常用的代码块可供使用,如下图 调用方法: (1)直接拖到代码区域中: (2)使用快捷键,键入 “while”, Xcode就会出现自动完成的提示 也可以自定义自己常用的代码块 ...
- Android 上的一些profiler tools
cpu这边先配了一个unity自带的profiler https://docs.unity3d.com/560/Documentation/Manual/ProfilerWindow.html 连an ...
- 【Hadoop】伪分布式环境搭建、验证
Hadoop伪分布式环境搭建: 自动部署脚本: #!/bin/bash set -eux export APP_PATH=/opt/applications export APP_NAME=Ares ...
- 2017.11.15 linux软件安装管理(todo)
学习来自:http://www.imooc.com/learn/447 第一章 介绍 第二章 软件包简介 1.源码包 2.二进制包(RPM包或系统默认包) 脚本安装包其实是别人把软件安装的脚本写好了, ...
- [Flutter] Creating, Importing & Using Dynamic Widgets from Other Files in a Flutter Application
In this lesson we’ll learn how to import widgets we’ve created in other files & use them in our ...
- Sublime Theme
Package Control Messages======================== Theme - Spacegray----------------- Thanks for insta ...
- $ 和getElementId的区别 / 一个jquery对象的原型
请说出 div 和 $div 的联系和区别 区别 div 返回一个HTML DOM Object $div 返回一个 jQuery Object, 两者不等价 $div是包装了dom对象后产生的,无法 ...
- TP框架中模糊查询实现
TP框架中模糊查询实现 $where['g.name'] = array('like','%'.$groupname.'%'); 表达式查询 上面的查询条件仅仅是一个简单的相等判断,可以使用查询表达式 ...
- Jmeter变量参数化及函数应用
分类: 测试工具 2006-12-14 10:54 12041人阅读 评论(5) 收藏 举报 javascriptloadrunnerrandom脚本测试多线程 我们在使用Jmeter录制脚本后,经常 ...