今天看到一篇好文章

https://www.2cto.com/kf/201609/545979.html

转载过来记录一下,往后需要的时候可以随时查看;

接下来进入正题:

动态fragment的使用

对于动态的使用fragment,就是简单的底部多个按钮,然后多个fragment进行切换,这个应该很简单,平时都在用。

MainActivity:

/**
* 主布局
* @author Rine
* @version 1.0, 2015-12-1
*/
public class MainActivity extends FragmentActivity{
MainDB mdata = new MainDB(); /**
* 定义结束时间
*/
private long exitTime = 0; /**
* 定义FragmentTabHost对象
*/
private FragmentTabHost mTabHost; /**
* 定义一个布局
*/
private LayoutInflater layoutInflater; /**
* 定义数组来存放Fragment界面
*/
private Class fragmentArray[] = mdata.fragmentArray; /**
* 定义数组来存放按钮图片
*/
private int mImageViewArray[] = mdata.ImageViewArray; /**
* Tab选项卡的文字
*/
private String mTextviewArray[] = mdata.TextviewArray; @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// CloudOnlyDB dbHelper;
// dbHelper = new CloudOnlyDB(MainActivity.this);
// HomeData home = new HomeData(MainActivity.this);
initView( );
} /**
* 初始化组件
*/
private void initView (){
/**
* 实例化布局对象
*/
layoutInflater = LayoutInflater.from(this); /**
* 实例化TabHost对象,得到TabHost
*/
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.container); //加载内容 /**
* 得到fragment的个数
*/
int count = fragmentArray.length; for(int i = 0; i < count; i++){
TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i)); //为每一个Tab按钮设置图标、文字和内容
mTabHost.addTab(tabSpec, fragmentArray[i], null); //将Tab按钮添加进Tab选项卡中
} } /**
* 给Tab按钮设置图标和文字
*/
private View getTabItemView(int index){
View view = layoutInflater.inflate(R.layout.main_tab_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(mImageViewArray[index]);
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(mTextviewArray[index]);
return view;
} /**
* 2次退出效果
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
exit(); //按返回键,true则退出
return false;
}
return super.onKeyDown(keyCode, event);
} public void exit() { //按返回退出
if ((System.currentTimeMillis() - exitTime) > 2000) {
Toast.makeText(getApplicationContext(), "再按一次退出程序",
Toast.LENGTH_SHORT).show();
exitTime = System.currentTimeMillis();
} else { //退出
finish();
System.exit(0);
} } }

MainAcitivity 中的MainDB

/**
* 存放主布局数据
* @author Rine
* @version 1.0, 2015-12-1
*/
public class MainDB {
/**
* 定义数组来存放Fragment界面
*/
public Class fragmentArray[] = {one.class,two.class,three.class};
/**
* 定义数组来存放按钮图片
*/
public int ImageViewArray[] = {R.drawable.home,R.drawable.ccarticle,
R.drawable.ccone};
/**
* Tab选项卡的文字
*/
public String TextviewArray[] = {"one","twom","three"};
}

MainActivity 相应的布局:

<!--?xml version="1.0" encoding="utf-8"?-->
<!-- Rine -->
<!-- 功能:主布局 --> <linearlayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android"> <framelayout android:id="@+id/container" android:layout_height="0dp" android:layout_weight="1" android:layout_width="fill_parent"> <framelayout android:id="@android:id/tabcontent" android:layout_height="0dp" android:layout_weight="0" android:layout_width="0dp">
</framelayout></android.support.v4.app.fragmenttabhost> </framelayout></linearlayout>

相应的style:

<style name="homework_tab_item" type="text/css"><item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:button">@null</item>
<item name="android:gravity">center</item>
<item name="android:textColor">@color/homework_tab_item_text_color</item></style>

相应的 color:

<!--?xml version="1.0" encoding="utf-8"?-->
<selector xmlns:android="https://schemas.android.com/apk/res/android"> <item android:color="#000000" android:state_checked="true">
<item android:color="#757575"> </item></item></selector>

fragment嵌套fragment

这就是简单动态fragment的应该。这也是其中的一种方法。

然后就是在其fragment中再嵌套多个fragment,我采用的是利用单选按钮来实现,当然单选按钮外面还要包一层RadioGroup。

Activity:

/**
* fragment嵌套fragment
* @author Rine
* @version 1.0, 2015-12-1
*/
public class two extends Fragment implements OnClickListener
{
/**
* 定义一个布局
*/
private LayoutInflater inflater;
// private View rootView;// 缓存Fragment view
private Context mainActivity;
private TwoToOne twoToOne;
private TwoToTwo twoToTwo;
/**
* one、two RadioGroup 控件
*/
private RadioGroup twoGroup;
protected RadioButton twoOne, twoTwo; /**
* 加载页面
*/
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mainActivity = getActivity();
inflater = LayoutInflater.from(getActivity());
// 初始化控件和声明事件
// rootView = inflater.inflate(R.layout.two, null);
twoGroup = (RadioGroup) getActivity().findViewById(R.id.two_group);
twoOne = (RadioButton) getActivity().findViewById(R.id.two_one);
twoTwo = (RadioButton) getActivity().findViewById(R.id.two_two);
//控件颜色
twoOne.setTextColor(getResources().getColor(R.color.red));
twoTwo.setTextColor(getResources().getColor(R.color.black));
twoOne.setOnClickListener(this);
twoTwo.setOnClickListener(this); setDefaultFragment();
}
/**
* 设置默认的Fragment
*/
private void setDefaultFragment()
{
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
twoToOne = new TwoToOne();
transaction.add(R.id.framelayout, twoToOne).commit();
} @Override
public void onClick(View v) {
FragmentManager fm = getFragmentManager();
// 开启Fragment事务
FragmentTransaction transaction = fm.beginTransaction(); switch (v.getId())
{
case R.id.two_one:
if (twoToOne == null)
{
twoToOne = new TwoToOne();
}
// 使用当前Fragment的布局替代id_content的控件
transaction.replace(R.id.framelayout, twoToOne);
//控件颜色
twoOne.setTextColor(getResources().getColor(R.color.red));
twoTwo.setTextColor(getResources().getColor(R.color.black));
break;
case R.id.two_two:
if (twoToTwo == null)
{
twoToTwo = new TwoToTwo();
}
transaction.replace(R.id.framelayout, twoToTwo);
//控件颜色
twoOne.setTextColor(getResources().getColor(R.color.black));
twoTwo.setTextColor(getResources().getColor(R.color.red));
break;
}
// transaction.addToBackStack();
// 事务提交
transaction.commit();
} @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.two, null); } }

其对应的布局:

<!--?xml version="1.0" encoding="utf-8"?-->
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="https://schemas.android.com/apk/res/android">
<relativelayout android:background="@color/white" android:layout_height="@dimen/height_title" android:layout_width="match_parent">
<linearlayout android:id="@+id/layout_brck" android:layout_height="match_parent" android:layout_marginleft="@dimen/margin_15dp" android:layout_width="wrap_content" android:orientation="horizontal"> </linearlayout>
<radiogroup android:gravity="center" android:id="@+id/two_group" android:layout_centerinparent="true" android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="horizontal"> <radiobutton android:background="@null" android:checked="true" android:id="@+id/two_one" android:layout_marginright="@dimen/margin_20dp" android:text="one" android:textcolor="@color/black" android:textsize="@dimen/title_textsize_22sp" style="@style/homework_tab_item"> <radiobutton android:background="@null" android:id="@+id/two_two" android:layout_margin="5dp" android:layout_marginleft="@dimen/margin_20dp" android:text="two" android:textcolor="@color/black" android:textsize="@dimen/title_textsize_22sp" style="@style/homework_tab_item">
</radiobutton></radiobutton></radiogroup> <view android:background="@color/br_title_color" android:layout_alignparentbottom="true" android:layout_height="0.1dp" android:layout_width="match_parent">
</view></relativelayout> <framelayout android:id="@+id/framelayout" android:layout_height="0dp" android:layout_weight="1" android:layout_width="match_parent">
</framelayout> </linearlayout>

OK。大体就是这样了。示例图如下:

关于Fragment里面嵌套fragment的更多相关文章

  1. Fragment里面嵌套Fragment的问题

    最近两天做项目时,要在fragment里面嵌套Fragment,最开始使用Fragment的hide,show等方法一直失败,,如图,message是一个fragment,在里面又有两个子fragme ...

  2. 关于TabLayout与ViewPager在Fragment中嵌套Fragment使用或配合使用的思考

    注意: 因为继承的是Fragment,所以getSupportFragmentManager()与getFragmentManager()方法无法使用,这里需要用到getChildFragmentMa ...

  3. Android 中关于Fragment嵌套Fragment的问题

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/5802146.html 问题描述: 在项目中Activity A中嵌套Fragment B,Fragment ...

  4. android fragment嵌套fragment出现的问题:no activity

    package com.example.fragmentNavigation2.fragment; import android.content.Context; import android.os. ...

  5. 嵌套fragment时必须要重写 onDetach()

    /**     * 嵌套fragment时必须要重写 onDetach()如下     */ @Override public void onDetach() { super.onDetach(); ...

  6. fragment中嵌套viewpager,vierpager中有多个fragment,不显示 .

    fragment中嵌套viewpager,vierpager中有多个fragment,不显示 ... 现在好多应用流行一种布局.底部几个工具栏选项,上面也有类似tab的选项. 底部用RadioGrou ...

  7. Fragment嵌套Fragment时候。子类fragment调用父容器Fragment方法

    业务场景:有的时候我们的页面可能是Activity 嵌套多个Fragment ..其中某个Fragment 又嵌套多个Fragment. 其中某个子Fragment  定义为  NewsFragmen ...

  8. Android FragmentActivity 嵌套 Fragment 调用startActivityForResult返回的requestCode错误

    Android FragmentActivity 嵌套 Fragment 调用startActivityForResult返回的requestCode错误 此时,要在调用startActivityFo ...

  9. Android流行界面结构——Fragment通过ViewPager(带指示器)嵌套Fragment结构的创建方法详解

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6287213.html 当前Android流行界面结构的一种——Fragment通过ViewPage ...

随机推荐

  1. Dotnet全平台下APM-Trace探索

    背景 随着支撑的内部业务系统越来越多,向着服务化架构进化,在整个迭代过程中,会逐渐暴露出以下问题. 传统依赖于应用服务器日志等手段的排除故障原因的复杂度越来越高,传统的监控服务已经无法满足需求. 终端 ...

  2. Hibernate工具类_抽取重复核心代码

    问题:在Hibernate中每次执行一次操作总是需要加载核心配置文件,获取连接池等等都是重复动作,所以抽取出来 解决: package com.xxx.utils; /** *Hibernate的工具 ...

  3. boostrap中模态框显示在阴影之下

    boostrap中模态框显示在阴影之下 出现这种情况的原因我开始也搞了很久,问题出现在哪里呢? 有事问百度,在百度上查了一下资料,他们主要的解决办法:是 修改标签的z-index属性的值, 我试着改了 ...

  4. Android 程序结构

    Android程序在创建的时,Android studio就为其构建了基本结构,设计者可在此结构上开发应用程序, manifests :用于存放AndroidManifest.xml文件(又称清单文件 ...

  5. Windows Server 2016-DNS客户端新增功能

    域名系统(DNS)是包含TCP / IP的行业标准协议套件之一,DNS客户端和DNS服务器一起为计算机和用户提供计算机名称到IP地址映射名称解析服务. 在Windows Server 2016中,DN ...

  6. 3星|路江涌《共演战略画布》:PPT技巧级别的创新,缺实际分析案例

    作者用自己的思路综合现有各种战略思想,给出企业各阶段各要素的战略分析工具.主要是2*2矩阵和双S曲线两种工具. 从书中的插图来看,这些工具在PPT演示中效果应该会不错. 作者在书中用这些工具做的分析不 ...

  7. NLP第八条

    今日!我虽啥也没干,但我还是有着学习的心的~          也许是“遵义会议”呢   那也说不定 留下这句话再说 哈哈哈哈哈哈 只能抄抄NLP第八条了  谁叫我选了个灰常有意义的通识课咧 八,每一 ...

  8. Java原子类操作原理剖析

    ◆CAS的概念◆ 对于并发控制来说,使用锁是一种悲观的策略.它总是假设每次请求都会产生冲突,如果多个线程请求同一个资源,则使用锁宁可牺牲性能也要保证线程安全.而无锁则是比较乐观的看待这个问题,它会假设 ...

  9. 【Python实践-3】汉诺塔问题递归求解(打印移动步骤及计算移动步数)

    # -*- coding: utf-8 -*- #汉诺塔移动问题 # 定义move(n,a,b,c)函数,接受参数n,表示3个柱子A.B.C中第1个柱子A的盘子数量 # 然后打印出把所有盘子从A借助B ...

  10. Linux下Springboot解决`APR based Apache Tomcat Native library`提示

    最近转行做java,开发基于Springboot的项目,版本号为2.1.0.RELEASE. 启动应用,发现以下提示: The APR based Apache Tomcat Native libra ...