react-native-navigation的迁移库

TitleBarButton.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package com.reactnativenavigation.views;
  2. import android.support.annotation.NonNull;
  3. import android.support.annotation.Nullable;
  4. import android.view.Menu;
  5. import android.view.MenuItem;
  6. import android.view.View;
  7. import android.widget.TextView;
  8. import com.reactnativenavigation.NavigationApplication;
  9. import com.reactnativenavigation.params.TitleBarButtonParams;
  10. import com.reactnativenavigation.utils.ViewUtils;
  11. import java.util.ArrayList;
  12. class TitleBarButton implements MenuItem.OnMenuItemClickListener {
  13. protected final Menu menu;
  14. protected final View parent;
  15. TitleBarButtonParams buttonParams;
  16. @Nullable protected String navigatorEventId;
  17. TitleBarButton(Menu menu, View parent, TitleBarButtonParams buttonParams, @Nullable String navigatorEventId) {
  18. this.menu = menu;
  19. this.parent = parent;
  20. this.buttonParams = buttonParams;
  21. this.navigatorEventId = navigatorEventId;
  22. }
  23. MenuItem addToMenu(int index) {
  24. MenuItem item = menu.add(Menu.NONE, Menu.NONE, index, buttonParams.label);
  25. item.setShowAsAction(buttonParams.showAsAction.action);
  26. item.setEnabled(buttonParams.enabled);
  27. setIcon(item);
  28. setColor();
  29. item.setOnMenuItemClickListener(this);
  30. return item;
  31. }
  32. private void setIcon(MenuItem item) {
  33. if (hasIcon()) {
  34. item.setIcon(buttonParams.icon);
  35. }
  36. }
  37. private void setColor() {
  38. if (!hasColor()) {
  39. return;
  40. }
  41. if (hasIcon()) {
  42. setIconColor();
  43. } else {
  44. setTextColor();
  45. }
  46. }
  47. private void setIconColor() {
  48. ViewUtils.tintDrawable(buttonParams.icon, buttonParams.color.getColor(), buttonParams.enabled);
  49. }
  50. private void setTextColor() {
  51. ViewUtils.runOnPreDraw(parent, new Runnable() {
  52. @Override
  53. public void run() {
  54. ArrayList<View> outViews = findActualTextViewInMenuByLabel();
  55. setTextColorForFoundButtonViews(outViews);
  56. }
  57. });
  58. }
  59. @NonNull
  60. private ArrayList<View> findActualTextViewInMenuByLabel() {
  61. ArrayList<View> outViews = new ArrayList<>();
  62. parent.findViewsWithText(outViews, buttonParams.label, View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
  63. return outViews;
  64. }
  65. private void setTextColorForFoundButtonViews(ArrayList<View> outViews) {
  66. for (View button : outViews) {
  67. ((TextView) button).setTextColor(buttonParams.getColor().getColor());
  68. }
  69. }
  70. private boolean hasIcon() {
  71. return buttonParams.icon != null;
  72. }
  73. private boolean hasColor() {
  74. return buttonParams.color.hasColor();
  75. }
  76. @Override
  77. public boolean onMenuItemClick(MenuItem item) {
  78. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent(buttonParams.eventId, navigatorEventId);
  79. return true;
  80. }
  81. }