react-native-navigation的迁移库

TitleBar.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.reactnativenavigation.views;
  2. import android.animation.Animator;
  3. import android.animation.AnimatorListenerAdapter;
  4. import android.content.Context;
  5. import android.graphics.drawable.Drawable;
  6. import android.support.annotation.Nullable;
  7. import android.support.v7.widget.ActionMenuView;
  8. import android.support.v7.widget.Toolbar;
  9. import android.view.Menu;
  10. import android.view.View;
  11. import android.view.animation.AccelerateDecelerateInterpolator;
  12. import android.view.animation.AccelerateInterpolator;
  13. import com.reactnativenavigation.params.BaseTitleBarButtonParams;
  14. import com.reactnativenavigation.params.StyleParams;
  15. import com.reactnativenavigation.params.TitleBarButtonParams;
  16. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  17. import com.reactnativenavigation.utils.ViewUtils;
  18. import java.util.List;
  19. public class TitleBar extends Toolbar {
  20. private LeftButton leftButton;
  21. private ActionMenuView actionMenuView;
  22. public TitleBar(Context context) {
  23. super(context);
  24. }
  25. @Override
  26. public void onViewAdded(View child) {
  27. super.onViewAdded(child);
  28. if (child instanceof ActionMenuView) {
  29. actionMenuView = (ActionMenuView) child;
  30. }
  31. }
  32. public void setRightButtons(List<TitleBarButtonParams> rightButtons, String navigatorEventId) {
  33. Menu menu = getMenu();
  34. menu.clear();
  35. if (rightButtons == null) {
  36. return;
  37. }
  38. addButtonsToTitleBar(rightButtons, navigatorEventId, menu);
  39. }
  40. public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
  41. LeftButtonOnClickListener leftButtonOnClickListener,
  42. String navigatorEventId,
  43. boolean overrideBackPressInJs) {
  44. if (shouldSetLeftButton(leftButtonParams)) {
  45. createAndSetLeftButton(leftButtonParams, leftButtonOnClickListener, navigatorEventId, overrideBackPressInJs);
  46. } else if (hasLeftButton()) {
  47. updateLeftButton(leftButtonParams);
  48. }
  49. }
  50. public void setStyle(StyleParams params) {
  51. setVisibility(params.titleBarHidden ? GONE : VISIBLE);
  52. setTitleTextColor(params);
  53. setSubtitleTextColor(params);
  54. colorOverflowButton(params);
  55. setTranslucent(params);
  56. }
  57. private void colorOverflowButton(StyleParams params) {
  58. Drawable overflowIcon = actionMenuView.getOverflowIcon();
  59. if (shouldColorOverflowButton(params, overflowIcon)) {
  60. ViewUtils.tintDrawable(overflowIcon, params.titleBarButtonColor.getColor(), true);
  61. }
  62. }
  63. private void setTranslucent(StyleParams params) {
  64. if (params.topBarTranslucent) {
  65. setBackground(new TranslucentTitleBarBackground());
  66. }
  67. }
  68. private boolean shouldColorOverflowButton(StyleParams params, Drawable overflowIcon) {
  69. return overflowIcon != null && params.titleBarButtonColor.hasColor();
  70. }
  71. protected void setTitleTextColor(StyleParams params) {
  72. if (params.titleBarTitleColor.hasColor()) {
  73. setTitleTextColor(params.titleBarTitleColor.getColor());
  74. }
  75. }
  76. protected void setSubtitleTextColor(StyleParams params) {
  77. if (params.titleBarSubtitleColor.hasColor()) {
  78. setSubtitleTextColor(params.titleBarSubtitleColor.getColor());
  79. }
  80. }
  81. private void addButtonsToTitleBar(List<TitleBarButtonParams> rightButtons, String navigatorEventId, Menu menu) {
  82. for (int i = 0; i < rightButtons.size(); i++) {
  83. final TitleBarButton button = ButtonFactory.create(menu, this, rightButtons.get(i), navigatorEventId);
  84. addButtonInReverseOrder(rightButtons, i, button);
  85. }
  86. }
  87. protected void addButtonInReverseOrder(List<? extends BaseTitleBarButtonParams> buttons, int i, TitleBarButton button) {
  88. final int index = buttons.size() - i - 1;
  89. button.addToMenu(index);
  90. }
  91. private boolean hasLeftButton() {
  92. return leftButton != null;
  93. }
  94. private void updateLeftButton(TitleBarLeftButtonParams leftButtonParams) {
  95. leftButton.setIconState(leftButtonParams);
  96. }
  97. private boolean shouldSetLeftButton(TitleBarLeftButtonParams leftButtonParams) {
  98. return leftButton == null && leftButtonParams != null;
  99. }
  100. private void createAndSetLeftButton(TitleBarLeftButtonParams leftButtonParams,
  101. LeftButtonOnClickListener leftButtonOnClickListener,
  102. String navigatorEventId,
  103. boolean overrideBackPressInJs) {
  104. leftButton = new LeftButton(getContext(), leftButtonParams, leftButtonOnClickListener, navigatorEventId,
  105. overrideBackPressInJs);
  106. setNavigationOnClickListener(leftButton);
  107. setNavigationIcon(leftButton);
  108. }
  109. public void hide() {
  110. hide(null);
  111. }
  112. public void hide(@Nullable final Runnable onHidden) {
  113. animate()
  114. .alpha(0)
  115. .setDuration(200)
  116. .setInterpolator(new AccelerateInterpolator())
  117. .setListener(new AnimatorListenerAdapter() {
  118. @Override
  119. public void onAnimationEnd(Animator animation) {
  120. if (onHidden != null) {
  121. onHidden.run();
  122. }
  123. }
  124. });
  125. }
  126. public void show() {
  127. this.show(null);
  128. }
  129. public void show(final @Nullable Runnable onDisplayed) {
  130. setAlpha(0);
  131. animate()
  132. .alpha(1)
  133. .setDuration(200)
  134. .setInterpolator(new AccelerateDecelerateInterpolator())
  135. .setListener(new AnimatorListenerAdapter() {
  136. @Override
  137. public void onAnimationEnd(Animator animation) {
  138. if (onDisplayed != null) {
  139. onDisplayed.run();
  140. }
  141. }
  142. });
  143. }
  144. }