react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.reactnativenavigation.layouts;
  2. import android.annotation.TargetApi;
  3. import android.app.Activity;
  4. import android.content.Context;
  5. import android.graphics.Color;
  6. import android.os.Build;
  7. import android.util.Log;
  8. import android.view.View;
  9. import android.view.Window;
  10. import android.widget.RelativeLayout;
  11. import com.reactnativenavigation.animation.OnScrollAnimator;
  12. import com.reactnativenavigation.params.ScreenParams;
  13. import com.reactnativenavigation.params.ScreenStyleParams;
  14. import com.reactnativenavigation.utils.SdkSupports;
  15. import com.reactnativenavigation.views.ContentView;
  16. import com.reactnativenavigation.views.ScrollDirectionListener;
  17. import com.reactnativenavigation.views.TopBar;
  18. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  19. import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
  20. public class ScreenImpl extends RelativeLayout implements Screen, ScrollDirectionListener.OnScrollChanged {
  21. private final ScreenParams screenParams;
  22. private ContentView contentView;
  23. private TopBar topBar;
  24. private OnScrollAnimator scrollAnimator;
  25. public ScreenImpl(Context context, ScreenParams screenParams) {
  26. super(context);
  27. this.screenParams = screenParams;
  28. createViews();
  29. setStyle(screenParams.styleParams);
  30. }
  31. private void createViews() {
  32. addTopBar();
  33. addTitleBar();
  34. addContentView();
  35. }
  36. private void addTitleBar() {
  37. topBar.addTitleBarAndSetButtons(screenParams.buttons, screenParams.navigatorEventId);
  38. topBar.setTitle(screenParams.title);
  39. }
  40. private void addTopBar() {
  41. topBar = new TopBar(getContext());
  42. addView(topBar, new LayoutParams(MATCH_PARENT, WRAP_CONTENT));
  43. }
  44. private void addContentView() {
  45. contentView = new ContentView(getContext(), screenParams, this);
  46. RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(MATCH_PARENT, MATCH_PARENT);
  47. if (!screenParams.styleParams.drawUnderTopBar) {
  48. params.addRule(RelativeLayout.BELOW, topBar.getId());
  49. }
  50. addView(contentView, params);
  51. contentView.init();
  52. }
  53. private void setStyle(ScreenStyleParams styleParams) {
  54. setStatusBarColor(styleParams.statusBarColor);
  55. setTopBarColor(styleParams.topBarColor);
  56. setNavigationBarColor(styleParams.navigationBarColor);
  57. topBar.setTitleBarVisibility(styleParams.titleBarHidden);
  58. topBar.setVisibility(styleParams.topBarHidden ? GONE : VISIBLE);
  59. }
  60. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  61. private void setStatusBarColor(ScreenStyleParams.Color statusBarColor) {
  62. if (!SdkSupports.lollipop()) {
  63. return;
  64. }
  65. final Activity context = (Activity) getContext();
  66. final Window window = context.getWindow();
  67. if (statusBarColor.hasColor()) {
  68. window.setStatusBarColor(statusBarColor.getColor());
  69. } else {
  70. window.setStatusBarColor(Color.BLACK);
  71. }
  72. }
  73. private void setTopBarColor(ScreenStyleParams.Color topBarColor) {
  74. if (topBarColor.hasColor()) {
  75. topBar.setBackgroundColor(topBarColor.getColor());
  76. }
  77. }
  78. @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  79. public void setNavigationBarColor(ScreenStyleParams.Color navigationBarColor) {
  80. if (!SdkSupports.lollipop()) {
  81. return;
  82. }
  83. final Activity context = (Activity) getContext();
  84. final Window window = context.getWindow();
  85. if (navigationBarColor.hasColor()) {
  86. window.setNavigationBarColor(navigationBarColor.getColor());
  87. } else {
  88. window.setNavigationBarColor(Color.BLACK);
  89. }
  90. }
  91. @Override
  92. public void onScrollChanged(ScrollDirectionListener.Direction direction) {
  93. Log.d("TAG", "onScrollChanged: ");
  94. if (scrollAnimator == null) {
  95. scrollAnimator = new OnScrollAnimator(topBar, OnScrollAnimator.HideDirection.Up, topBar.getHeight());
  96. }
  97. scrollAnimator.onScrollChanged(direction);
  98. }
  99. @Override
  100. public View asView() {
  101. return this;
  102. }
  103. @Override
  104. public void ensureUnmountOnDetachedFromWindow() {
  105. contentView.ensureUnmountOnDetachedFromWindow();
  106. }
  107. @Override
  108. public void preventUnmountOnDetachedFromWindow() {
  109. contentView.preventUnmountOnDetachedFromWindow();
  110. }
  111. }