react-native-navigation的迁移库

ContextualMenu.java 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. package com.reactnativenavigation.views;
  2. import android.content.Context;
  3. import android.view.Menu;
  4. import android.view.ViewManager;
  5. import com.facebook.react.bridge.Callback;
  6. import com.reactnativenavigation.NavigationApplication;
  7. import com.reactnativenavigation.events.ContextualMenuHiddenEvent;
  8. import com.reactnativenavigation.events.EventBus;
  9. import com.reactnativenavigation.params.ContextualMenuButtonParams;
  10. import com.reactnativenavigation.params.ContextualMenuParams;
  11. import com.reactnativenavigation.params.StyleParams;
  12. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  13. import java.util.List;
  14. public class ContextualMenu extends TitleBar implements LeftButtonOnClickListener, ContextualMenuButton.ContextualButtonClickListener {
  15. private ContextualMenuParams params;
  16. private Callback onButtonClicked;
  17. private final String navigatorEventId;
  18. public ContextualMenu(Context context, ContextualMenuParams params, StyleParams styleParams, Callback onButtonClicked) {
  19. super(context);
  20. this.params = params;
  21. this.onButtonClicked = onButtonClicked;
  22. navigatorEventId = params.navigationParams.navigatorEventId;
  23. setStyle(styleParams);
  24. setButtons();
  25. }
  26. public void setStyle(StyleParams styleParams) {
  27. params.setButtonsColor(styleParams.contextualMenuButtonsColor);
  28. if (styleParams.contextualMenuBackgroundColor.hasColor()) {
  29. setBackgroundColor(styleParams.contextualMenuBackgroundColor.getColor());
  30. }
  31. }
  32. public void setButtons() {
  33. addButtonsToContextualMenu(params.buttons, getMenu());
  34. setBackButton(params.leftButton);
  35. }
  36. private void setBackButton(TitleBarLeftButtonParams leftButton) {
  37. setLeftButton(leftButton, this, null, false);
  38. }
  39. private void addButtonsToContextualMenu(List<ContextualMenuButtonParams> buttons, Menu menu) {
  40. for (int i = 0; i < buttons.size(); i++) {
  41. final TitleBarButton button = new ContextualMenuButton(menu, this, buttons.get(i), this);
  42. addButtonInReverseOrder(buttons, i, button);
  43. }
  44. }
  45. @Override
  46. public boolean onTitleBarBackButtonClick() {
  47. dismiss();
  48. EventBus.instance.post(new ContextualMenuHiddenEvent());
  49. return true;
  50. }
  51. @Override
  52. public void onSideMenuButtonClick() {
  53. // nothing
  54. }
  55. @Override
  56. public void onClick(int index) {
  57. dismiss();
  58. EventBus.instance.post(new ContextualMenuHiddenEvent());
  59. onButtonClicked.invoke(index);
  60. }
  61. public void dismiss() {
  62. hide(new Runnable() {
  63. @Override
  64. public void run() {
  65. ((ViewManager) getParent()).removeView(ContextualMenu.this);
  66. }
  67. });
  68. NavigationApplication.instance.getEventEmitter().sendNavigatorEvent("contextualMenuDismissed", navigatorEventId);
  69. }
  70. }