react-native-navigation的迁移库

ScreenStack.java 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. package com.reactnativenavigation.views;
  2. import android.animation.LayoutTransition;
  3. import android.content.Context;
  4. import android.util.AttributeSet;
  5. import android.widget.FrameLayout;
  6. import com.facebook.react.ReactInstanceManager;
  7. import com.facebook.react.ReactRootView;
  8. import com.reactnativenavigation.activities.BaseReactActivity;
  9. import com.reactnativenavigation.core.RctManager;
  10. import com.reactnativenavigation.core.objects.Screen;
  11. import com.reactnativenavigation.utils.ReflectionUtils;
  12. import java.util.Stack;
  13. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  14. public class ScreenStack extends FrameLayout {
  15. private static class ScreenView {
  16. Screen screen;
  17. RctView view;
  18. public ScreenView(Screen screen, RctView view) {
  19. this.screen = screen;
  20. this.view = view;
  21. }
  22. }
  23. private final Stack<ScreenView> mStack = new Stack<>();
  24. private final ReactInstanceManager mReactInstanceManager =
  25. RctManager.getInstance().getReactInstanceManager();
  26. private BaseReactActivity mReactActivity;
  27. public ScreenStack(BaseReactActivity context) {
  28. super(context);
  29. init(context);
  30. }
  31. public ScreenStack(Context context, AttributeSet attrs) {
  32. super(context, attrs);
  33. init(context);
  34. }
  35. private void init(Context context) {
  36. mReactActivity = (BaseReactActivity) context;
  37. setLayoutTransition(new LayoutTransition());
  38. }
  39. public void push(Screen screen) {
  40. push(screen, null);
  41. }
  42. public void push(Screen screen, RctView.OnDisplayedListener onDisplayed) {
  43. RctView oldView = null;
  44. if (!mStack.isEmpty()) {
  45. oldView = mStack.peek().view;
  46. }
  47. RctView view = new RctView(mReactActivity, mReactInstanceManager, screen, onDisplayed);
  48. addView(view, MATCH_PARENT, MATCH_PARENT);
  49. if (oldView != null) {
  50. ReactRootView reactRootView = oldView.getReactRootView();
  51. ReflectionUtils.setBooleanField(reactRootView, "mAttachScheduled", true);
  52. removeView(oldView);
  53. }
  54. mStack.push(new ScreenView(screen, view));
  55. }
  56. public Screen pop() {
  57. if (mStack.isEmpty() || getStackSize() == 1) {
  58. return null;
  59. }
  60. ScreenView popped = mStack.pop();
  61. addView(mStack.peek().view, 0);
  62. ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
  63. removeView(popped.view);
  64. return popped.screen;
  65. }
  66. public Screen popToRoot() {
  67. if (mStack.isEmpty() || getStackSize() <= 1) {
  68. return null;
  69. }
  70. ScreenView oldScreenView = null;
  71. while (getStackSize() > 1) {
  72. ScreenView popped = mStack.pop();
  73. ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
  74. removeView(popped.view);
  75. if (oldScreenView == null) {
  76. oldScreenView = popped;
  77. }
  78. }
  79. if (!mStack.isEmpty()) {
  80. addView(mStack.peek().view, 0);
  81. }
  82. return oldScreenView.screen;
  83. }
  84. public Screen resetTo(Screen screen) {
  85. return resetTo(screen, null);
  86. }
  87. public Screen resetTo(Screen screen, RctView.OnDisplayedListener onDisplayed) {
  88. RctView view = new RctView(mReactActivity, mReactInstanceManager, screen, onDisplayed);
  89. addView(view, MATCH_PARENT, MATCH_PARENT);
  90. ScreenView oldScreenView = null;
  91. if (!mStack.isEmpty()) {
  92. while (getStackSize() > 0) {
  93. ScreenView screenView = mStack.pop();
  94. ReflectionUtils.setBooleanField(screenView.view.getReactRootView(), "mAttachScheduled", false);
  95. removeView(screenView.view);
  96. if (oldScreenView == null) {
  97. oldScreenView = screenView;
  98. }
  99. }
  100. }
  101. // Add screen to stack after it's clear
  102. mStack.push(new ScreenView(screen, view));
  103. if (oldScreenView == null) {
  104. return null;
  105. }
  106. return oldScreenView.screen;
  107. }
  108. public boolean isEmpty() {
  109. return mStack.isEmpty();
  110. }
  111. public int getStackSize() {
  112. return mStack.size();
  113. }
  114. public Screen peek() {
  115. return mStack.peek().screen;
  116. }
  117. }