react-native-navigation的迁移库

ScreenStack.java 2.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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()) {
  58. return null;
  59. }
  60. ScreenView popped = mStack.pop();
  61. if (!mStack.isEmpty()) {
  62. addView(mStack.peek().view, 0);
  63. }
  64. ReflectionUtils.setBooleanField(popped.view.getReactRootView(), "mAttachScheduled", false);
  65. removeView(popped.view);
  66. return popped.screen;
  67. }
  68. public boolean isEmpty() {
  69. return mStack.isEmpty();
  70. }
  71. public int getStackSize() {
  72. return mStack.size();
  73. }
  74. public Screen peek() {
  75. return mStack.peek().screen;
  76. }
  77. }