当前位置:   article > 正文

Android Fragment管理类,操作Fragment的增加、删除、替换、隐藏、显示等_android fragmentmanager移除最后一个fragment

android fragmentmanager移除最后一个fragment

Fragment管理类,操作Fragment的增加、删除、替换、隐藏、显示等

  1. package com.kayak.fragment;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import android.support.v4.app.Fragment;
  5. import android.support.v4.app.FragmentActivity;
  6. import android.support.v4.app.FragmentManager;
  7. import android.support.v4.app.FragmentTransaction;
  8. /**
  9. * @{# FragOperManager.java Create on 2015-5-15 下午3:59:49
  10. *
  11. * class desc: Fragment操作类 替换时删除id相同的fragment然
  12. * 后添加,只有一层,添加是多层
  13. * <p>
  14. * Copyright: Copyright(c) 2015
  15. * </p>
  16. * @Version 1.0
  17. * Tel 15211164134
  18. * Name 潘传爱
  19. * @Author <a href="15211164134@163.com">Hyca</a>
  20. *
  21. */
  22. public class FragOperManager {
  23. /**
  24. * FragmentActivity 实例
  25. */
  26. private FragmentActivity context;
  27. /**
  28. * Fragment 管理器
  29. */
  30. private FragmentManager fManager;
  31. /**
  32. * 装Fragment的容器
  33. */
  34. private int containerId;
  35. /**
  36. * 该Activity所有fragment的集合
  37. */
  38. private List<Fragment> fragments;
  39. /**
  40. * @param context FragmentActivity 实例
  41. * @param containerId 容器Id
  42. */
  43. public FragOperManager(FragmentActivity context, int containerId) {
  44. super();
  45. this.context = context;
  46. this.containerId = containerId;
  47. fManager = this.context.getSupportFragmentManager();
  48. fragments = new ArrayList<Fragment>();
  49. }
  50. /**
  51. * @param fragment 要替换的 fragment
  52. * @param tag fragment 标签
  53. * @param isBackStack 是否要回滚
  54. */
  55. public void chReplaceFrag(Fragment fragment,String tag,boolean isBackStack) {
  56. fragments.add(fragment);
  57. FragmentTransaction fTransaction = fManager.beginTransaction();
  58. fTransaction.replace(containerId, fragment, tag);
  59. if (isBackStack) {
  60. fTransaction.addToBackStack(tag);
  61. }
  62. fTransaction.commit();
  63. }
  64. /**
  65. * @param fragment 要替换的 fragment
  66. * @param tag fragment 标签
  67. * @param isBackStack 是否要回滚
  68. */
  69. public void chAddFrag(Fragment fragment,String tag,boolean isBackStack) {
  70. fragments.add(fragment);
  71. FragmentTransaction fTransaction = fManager.beginTransaction();
  72. fTransaction.add(containerId, fragment, tag);
  73. if (isBackStack) {
  74. fTransaction.addToBackStack(tag);
  75. }
  76. fTransaction.commit();
  77. }
  78. /**
  79. * 模拟按下返回键
  80. * @param name 返回到的Fragment的名字
  81. * @param flags 模式
  82. * FragmentManager.POP_BACK_STACK_INCLUSIVE
  83. * if name is null,flags is zero
  84. */
  85. public void goBack(String name, int flags) {
  86. fManager.popBackStack(name, flags);
  87. }
  88. /**
  89. * 通过tag获取到某个Fragment
  90. * @param tag 标签
  91. * @return
  92. */
  93. public Fragment getAllFragment(String tag) {
  94. return fManager.findFragmentByTag(tag);
  95. }
  96. /**
  97. * 删除某个Fragment
  98. * @param fragment 实例
  99. */
  100. public void chRemoveFrag(Fragment fragment) {
  101. if (fragment != null) {
  102. FragmentTransaction transaction = fManager.beginTransaction();
  103. transaction.remove(fragment);
  104. transaction.commit();
  105. }
  106. }
  107. /**
  108. * 隐藏Fragment 没有删除view
  109. * @param fragment
  110. */
  111. public void chHideFrag(Fragment fragment) {
  112. if (fragment != null) {
  113. FragmentTransaction transaction = fManager.beginTransaction();
  114. transaction.hide(fragment);
  115. transaction.commit();
  116. }
  117. }
  118. /**
  119. * 显示Fragment
  120. * @param fragment
  121. */
  122. public void chShowFrag(Fragment fragment) {
  123. if (fragment != null) {
  124. FragmentTransaction transaction = fManager.beginTransaction();
  125. transaction.show(fragment);
  126. transaction.commit();
  127. }
  128. }
  129. /**
  130. * 获取所有的Fragment
  131. * @return
  132. */
  133. public List<Fragment> getFragList() {
  134. return fragments;
  135. }
  136. /**
  137. * 获取Fragment管理器
  138. * @return
  139. */
  140. public FragmentManager getFragManager() {
  141. return fManager;
  142. }
  143. /**
  144. * 设置容器的Id
  145. * @param containerId
  146. */
  147. public void setContainerId(int containerId) {
  148. this.containerId = containerId;
  149. }
  150. }


声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/105756
推荐阅读
相关标签
  

闽ICP备14008679号