react-native-navigation的迁移库

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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 android.widget.TextView;
  14. import com.reactnativenavigation.params.BaseTitleBarButtonParams;
  15. import com.reactnativenavigation.params.StyleParams;
  16. import com.reactnativenavigation.params.TitleBarButtonParams;
  17. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  18. import com.reactnativenavigation.utils.ViewUtils;
  19. import java.util.List;
  20. public class TitleBar extends Toolbar {
  21. private static final int TITLE_VISIBILITY_ANIMATION_DURATION = 320;
  22. private LeftButton leftButton;
  23. private ActionMenuView actionMenuView;
  24. public TitleBar(Context context) {
  25. super(context);
  26. }
  27. @Override
  28. protected void onLayout(boolean changed, int l, int t, int r, int b) {
  29. super.onLayout(changed, l, t, r, b);
  30. }
  31. @Override
  32. public void onViewAdded(View child) {
  33. super.onViewAdded(child);
  34. if (child instanceof ActionMenuView) {
  35. actionMenuView = (ActionMenuView) child;
  36. }
  37. }
  38. public void setRightButtons(List<TitleBarButtonParams> rightButtons, String navigatorEventId) {
  39. Menu menu = getMenu();
  40. menu.clear();
  41. if (rightButtons == null) {
  42. return;
  43. }
  44. addButtonsToTitleBar(rightButtons, navigatorEventId, menu);
  45. }
  46. public void setLeftButton(TitleBarLeftButtonParams leftButtonParams,
  47. LeftButtonOnClickListener leftButtonOnClickListener,
  48. String navigatorEventId,
  49. boolean overrideBackPressInJs) {
  50. if (shouldSetLeftButton(leftButtonParams)) {
  51. createAndSetLeftButton(leftButtonParams, leftButtonOnClickListener, navigatorEventId, overrideBackPressInJs);
  52. } else if (hasLeftButton()) {
  53. if (leftButtonParams.hasIcon()) {
  54. updateLeftButton(leftButtonParams);
  55. } else {
  56. removeLeftButton();
  57. }
  58. }
  59. }
  60. private void removeLeftButton() {
  61. setNavigationIcon(null);
  62. leftButton = null;
  63. }
  64. public void setStyle(StyleParams params) {
  65. setVisibility(params.titleBarHidden ? GONE : VISIBLE);
  66. setTitleTextColor(params);
  67. setTitleTextFont(params);
  68. setSubtitleTextColor(params);
  69. colorOverflowButton(params);
  70. setBackground(params);
  71. centerTitle(params);
  72. }
  73. private void centerTitle(final StyleParams params) {
  74. final View titleView = getTitleView();
  75. ViewUtils.runOnPreDraw(titleView, new Runnable() {
  76. @Override
  77. public void run() {
  78. if (params.titleBarTitleTextCentered) {
  79. if (titleView != null) {
  80. int[] location = new int[2];
  81. titleView.getLocationOnScreen(location);
  82. titleView.setTranslationX(titleView.getTranslationX() + (-location[0] + ViewUtils.getScreenWidth() / 2 - titleView.getWidth() / 2));
  83. }
  84. }
  85. }
  86. });
  87. }
  88. private void colorOverflowButton(StyleParams params) {
  89. Drawable overflowIcon = actionMenuView.getOverflowIcon();
  90. if (shouldColorOverflowButton(params, overflowIcon)) {
  91. ViewUtils.tintDrawable(overflowIcon, params.titleBarButtonColor.getColor(), true);
  92. }
  93. }
  94. protected void setBackground(StyleParams params) {
  95. setTranslucent(params);
  96. }
  97. protected void setTranslucent(StyleParams params) {
  98. if (params.topBarTranslucent) {
  99. setBackground(new TranslucentDrawable());
  100. }
  101. }
  102. private boolean shouldColorOverflowButton(StyleParams params, Drawable overflowIcon) {
  103. return overflowIcon != null && params.titleBarButtonColor.hasColor();
  104. }
  105. protected void setTitleTextColor(StyleParams params) {
  106. if (params.titleBarTitleColor.hasColor()) {
  107. setTitleTextColor(params.titleBarTitleColor.getColor());
  108. }
  109. }
  110. protected void setTitleTextFont(StyleParams params) {
  111. if (params.titleBarTitleFont.hasFont()) {
  112. return;
  113. }
  114. View titleView = getTitleView();
  115. if (titleView instanceof TextView) {
  116. ((TextView) titleView).setTypeface(params.titleBarTitleFont.get());
  117. }
  118. }
  119. protected void setSubtitleTextColor(StyleParams params) {
  120. if (params.titleBarSubtitleColor.hasColor()) {
  121. setSubtitleTextColor(params.titleBarSubtitleColor.getColor());
  122. }
  123. }
  124. private void addButtonsToTitleBar(List<TitleBarButtonParams> rightButtons, String navigatorEventId, Menu menu) {
  125. for (int i = 0; i < rightButtons.size(); i++) {
  126. final TitleBarButton button = ButtonFactory.create(menu, this, rightButtons.get(i), navigatorEventId);
  127. addButtonInReverseOrder(rightButtons, i, button);
  128. }
  129. }
  130. protected void addButtonInReverseOrder(List<? extends BaseTitleBarButtonParams> buttons, int i, TitleBarButton button) {
  131. final int index = buttons.size() - i - 1;
  132. button.addToMenu(index);
  133. }
  134. private boolean hasLeftButton() {
  135. return leftButton != null;
  136. }
  137. private void updateLeftButton(TitleBarLeftButtonParams leftButtonParams) {
  138. leftButton.setIconState(leftButtonParams);
  139. }
  140. private boolean shouldSetLeftButton(TitleBarLeftButtonParams leftButtonParams) {
  141. return leftButton == null && leftButtonParams != null && leftButtonParams.iconState != null;
  142. }
  143. private void createAndSetLeftButton(TitleBarLeftButtonParams leftButtonParams,
  144. LeftButtonOnClickListener leftButtonOnClickListener,
  145. String navigatorEventId,
  146. boolean overrideBackPressInJs) {
  147. leftButton = new LeftButton(getContext(), leftButtonParams, leftButtonOnClickListener, navigatorEventId,
  148. overrideBackPressInJs);
  149. setNavigationOnClickListener(leftButton);
  150. if (leftButtonParams.icon != null) {
  151. setNavigationIcon(leftButtonParams.icon);
  152. } else {
  153. setNavigationIcon(leftButton);
  154. }
  155. }
  156. public void hide() {
  157. hide(null);
  158. }
  159. public void hide(@Nullable final Runnable onHidden) {
  160. animate()
  161. .alpha(0)
  162. .setDuration(200)
  163. .setInterpolator(new AccelerateInterpolator())
  164. .setListener(new AnimatorListenerAdapter() {
  165. @Override
  166. public void onAnimationEnd(Animator animation) {
  167. if (onHidden != null) {
  168. onHidden.run();
  169. }
  170. }
  171. });
  172. }
  173. public void show() {
  174. this.show(null);
  175. }
  176. public void show(final @Nullable Runnable onDisplayed) {
  177. setAlpha(0);
  178. animate()
  179. .alpha(1)
  180. .setDuration(200)
  181. .setInterpolator(new AccelerateDecelerateInterpolator())
  182. .setListener(new AnimatorListenerAdapter() {
  183. @Override
  184. public void onAnimationEnd(Animator animation) {
  185. if (onDisplayed != null) {
  186. onDisplayed.run();
  187. }
  188. }
  189. });
  190. }
  191. public void showTitle() {
  192. animateTitle(1);
  193. }
  194. public void hideTitle() {
  195. animateTitle(0);
  196. }
  197. private void animateTitle(int alpha) {
  198. View titleView = getTitleView();
  199. if (titleView != null) {
  200. titleView.animate()
  201. .alpha(alpha)
  202. .setDuration(TITLE_VISIBILITY_ANIMATION_DURATION);
  203. }
  204. }
  205. @Nullable
  206. protected View getTitleView() {
  207. return ViewUtils.findChildByClass(this, TextView.class, new ViewUtils.Matcher<TextView>() {
  208. @Override
  209. public boolean match(TextView child) {
  210. return child.getText().equals(getTitle());
  211. }
  212. });
  213. }
  214. }