天天看点

FragmentTransaction 常用方法总结

1)add

1      

把fragment添加到activity指定的容器中

注意: fragment 必须是没有添加过的,获取是否已经添加使用

FragmentManager.findFragmentByTag(String)

2)attach

1      

把fragment重新添加到当前的 view hierarchy ,并显示,fragment 的onCreateView 会重新执行,布局重新绘制。

注意:fragment必须是经过detach

3)detach

1      

从当前的UI中分离fragment,fragment的视图结构会被破坏。

4) hide

1      

隐藏fragment,fragment必须是添加过的,只会隐藏当前的fragment。

5)remove

1      

如果container存在fragment,从container中移除 fragment。

6)replace

1      

相当先调用

remove (Fragment fragment)

,在调用

add(int containerViewId, Fragment fragment, String tag)

7)show

1      

显示之前

hide(fragment)

,同样fragment必须已经是添加过到container中。

fragment几个属性

1)

onAttach

onCreate

1
2
3      
//fragment第一次attach到当前的activity
onAttach(Activity activity)
onCreate(Bundle savedInstanceState)
      

2)onCreateView

1
2      
//创建视图
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
      

三种fragment使用情况

1)第一种

add()

+

attach()

+

detach()

fragment

上述

1

执行一次 ,

2

会在每次

attach()

执行

2)第二种

replace()

fragment

上术都会执行

3)第三种

add()

+

hide()

+

show()

fragment

上述执行一次

一些理解,如有不对,请留言...

声明:eoe文章著作权属于作者,受法律保护,转载时请务必以超链接形式附带如下信息

原文作者: 花_开_堪_折_枝

原文地址: http://my.eoe.cn/yaming/archive/2500.html