您的位置:首页 > 其它

利用fragmentManager实现tabhost效果同时保存fragment中数据

2016-03-28 19:00 405 查看
实现tabhost的效果方法很多,这次通过利用fragmentManager来实现,同时为了保存fragment中的数据,调用fragmentTransaction的hide和show方法来进行fragment的显示及隐藏!核心代码如下:

首先是布局代码

<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中的输入信息)。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: