利用fragmentManager实现tabhost效果同时保存fragment中数据
2016-03-28 19:00
405 查看
实现tabhost的效果方法很多,这次通过利用fragmentManager来实现,同时为了保存fragment中的数据,调用fragmentTransaction的hide和show方法来进行fragment的显示及隐藏!核心代码如下:
首先是布局代码
下面是activity中的核心代码段:
按钮响应代码段:
代码中old_index是指上一个显示的fragment的序号。
对于fragmentTransaction的一些方法,自己最近通过实践和参照网上的博客大概有了了解,下面就总结一下,如果您发现有什么问题,还希望能够指出!(下例以framelayout作为容器来说)
(1)add()与replace()
add()方法是向容器中添加一个fragment,如果之前已经有其他的fragment,那么就会重叠显示;生命周期中的方法onAttach()->onResume()
replace()方法向容器添加fragment之前会先把之前所有的fragment全部移除,再显示指定的fragment;其它fragment onPause()->onDetach(),添加的fragment onAttach()->onResume()
(2) hide()与show()
hide()与show()只是让fragment的界面隐藏或显示,对生命周期不会有影响
(3)addToBackStack()
首先addToBackStack()方法的拥有者是fragmentTransaction,所以当我们按返回键的时候,返回的是上一个事务,即显示的是上一个事务的执行结果,其中可能包括对多个fragment的操作。
当执行replace()方法时,如果在之前的fragment调用了addToBackStack(),那么按返回键将会显示上一个fragment(当然数据信息不会保存,比如EditText中的输入信息)。
首先是布局代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:id="@+id/fl_main" android:layout_marginBottom="50dp" android:layout_width="match_parent" android:layout_height="match_parent"> </FrameLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:orientation="horizontal"> <Button android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent" android:tag="1" android:text="按钮1" android:id="@+id/btn_main_1"/> <Button android:layout_width="match_parent" android:layout_weight="1" android:layout_height="match_parent" android:tag="2" android:text="按钮2" android:id="@+id/btn_main_2"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:tag="3" android:text="按钮3" android:id="@+id/btn_main_3"/> <Button android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" android:tag="4" android:text="按钮4" android:id="@+id/btn_main_4"/> </LinearLayout> </RelativeLayout>
下面是activity中的核心代码段:
Button tb_1 = (Button) this.findViewById(R.id.btn_main_1);//用来切换不同fragment Button tb_2 = (Button) this.findViewById(R.id.btn_main_2); Button tb_3 = (Button) this.findViewById(R.id.btn_main_3); Button tb_4 = (Button) this.findViewById(R.id.btn_main_4); tb_1.setOnClickListener(this); tb_2.setOnClickListener(this); tb_3.setOnClickListener(this); tb_4.setOnClickListener(this); fragmentManager = this.getFragmentManager(); firstFragment = new FirstFragment();//初始化要加载的fragments secondFragment = new SecondFragment(); thirdFragment = new ThirdFragment(); lastFragment = new LastFragment(); list = new ArrayList<Fragment>(); list.add(firstFragment);//开始先显示第一个fragment transaction = fragmentManager.beginTransaction(); transaction.add(R.id.fl_main, list.get(0), "first"); transaction.commit();
按钮响应代码段:
public void onClick(View v) { int tag = Integer.parseInt((String) v.getTag()); transaction = fragmentManager.beginTransaction(); switch (tag) { case 1: transaction.hide(list.get(old_index)); transaction.show(list.get(0)); transaction.commit(); old_index = 0; break; case 2: if (!list.contains(secondFragment)) {//先判断是否加载过 list.add(1, secondFragment); transaction.add(R.id.fl_main, secondFragment, "second"); } transaction.hide(list.get(old_index)); transaction.show(list.get(1)); transaction.commit(); old_index = 1; break; case 3: if (!list.contains(thirdFragment)) { list.add(2, thirdFragment); transaction.add(R.id.fl_main, thirdFragment, "third"); } transaction.hide(list.get(old_index)); transaction.show(list.get(2)); transaction.commit(); old_index = 2; break; case 4: if (!list.contains(lastFragment)) { list.add(3, lastFragment); transaction.add(R.id.fl_main, lastFragment, "last"); } transaction.hide(list.get(old_index)); transaction.show(list.get(3)); transaction.commit(); old_index = 3; break; } }
代码中old_index是指上一个显示的fragment的序号。
对于fragmentTransaction的一些方法,自己最近通过实践和参照网上的博客大概有了了解,下面就总结一下,如果您发现有什么问题,还希望能够指出!(下例以framelayout作为容器来说)
(1)add()与replace()
add()方法是向容器中添加一个fragment,如果之前已经有其他的fragment,那么就会重叠显示;生命周期中的方法onAttach()->onResume()
replace()方法向容器添加fragment之前会先把之前所有的fragment全部移除,再显示指定的fragment;其它fragment onPause()->onDetach(),添加的fragment onAttach()->onResume()
(2) hide()与show()
hide()与show()只是让fragment的界面隐藏或显示,对生命周期不会有影响
(3)addToBackStack()
首先addToBackStack()方法的拥有者是fragmentTransaction,所以当我们按返回键的时候,返回的是上一个事务,即显示的是上一个事务的执行结果,其中可能包括对多个fragment的操作。
当执行replace()方法时,如果在之前的fragment调用了addToBackStack(),那么按返回键将会显示上一个fragment(当然数据信息不会保存,比如EditText中的输入信息)。
相关文章推荐
- out_of_range 标准异常
- HTTP真的很简单
- jQuery
- No qualifying bean of type is defined: expected single matching bean but found 2
- linux的内建函数
- 为多种分辨率设计UI
- 使用 AVFoundation 实现视频倒序
- Android-Universal-Image-Loader 图片异步加载类库的使用(超详细配置)
- UI控件--ViewPager详解使用Fragment实现ViewPager滑动(4)
- AsyncTask用法
- OC一些重要知识点<三>
- 使用keras出现的一点问题
- MySQL 5.7 版本登录报 error 1524: plugin ... in not loaded
- 最大公约数
- 【JQuery】:first-child子元素过滤选择器
- 2016年3月26日作业
- Java in Tarena_Spring Note04
- 跟着Android学设计模式:代理(proxy)
- 搭建一个Web API项目(DDD)
- php如何处理这样的数据?