一、向容器中静态添加Fragment

FragmentManager  API

  • FragmentManager用来管理Activity中的Fragment
  • findFragmentByld()通过id查找Activity中的Fragment
  • findFragmentByTag()通过字符串查找Activity中的Fragment
  • popBackStack()模拟用户的点击Back键的操作
  • addOnBackStackChangedListener()添加后台栈改变监听器

①用id

Fragment的交互及管理

Fragment的交互及管理

Fragment的交互及管理

②用tag

Fragment的交互及管理

Fragment的交互及管理

 

二、向容器中动态添加Fragment

FragmentTransaction APl

  • add()向容器中添加一个Fragment
  • remove()删除一个Fragment
  • replace()将容器中的Fragment 替换成新的Fragment
  • hide()隐藏已存在的Fragment
  • show()显示此前隐藏的Fragment
  • addToBackStack()将事务添加到后台栈
  • commit()提交事务,将改变应用到Activit

添加:

Fragment的交互及管理

移除:

Fragment的交互及管理

隐藏:

Fragment的交互及管理

Fragment不可以带参构造,如果要设置相应的属性,可以用静态方法,初始化属性只在onCreate中进行的话,可以在静态方法中利用Buddle设置参数

Fragment的交互及管理

Fragment的交互及管理

Activity类里面使用静态方法产生对像,通过FragmentTransaction对象进行替换: 

Fragment的交互及管理

 

三、系统提供的Fragment

系统提供了几个其他的Fragment类开发者可以继承:

  • DialogFragment显示一个悬浮对话框。
  • ListFragment显示一个由adapter管理的项目列表。

DialogFragment:

Fragment的交互及管理

Fragment的交互及管理

Fragment的交互及管理

ListFragment:

Fragment的交互及管理

Fragment的交互及管理

现在Fragment的生命周期:

Fragment的交互及管理

 

相关文章:

  • 2022-01-07
  • 2022-12-23
  • 2022-12-23
  • 2021-09-29
  • 2021-04-25
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-07
  • 2022-01-06
  • 2022-12-23
  • 2021-09-04
  • 2021-12-01
  • 2022-01-08
  • 2022-12-23
相关资源
相似解决方案