react-native-navigation的迁移库

TopBar.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. package com.reactnativenavigation.views;
  2. import android.content.Context;
  3. import android.graphics.Color;
  4. import android.os.Build;
  5. import android.support.design.widget.AppBarLayout;
  6. import android.support.design.widget.TabLayout;
  7. import android.view.ViewGroup;
  8. import android.widget.FrameLayout;
  9. import com.facebook.react.bridge.Callback;
  10. import com.reactnativenavigation.params.ContextualMenuParams;
  11. import com.reactnativenavigation.params.StyleParams;
  12. import com.reactnativenavigation.params.TitleBarButtonParams;
  13. import com.reactnativenavigation.params.TitleBarLeftButtonParams;
  14. import com.reactnativenavigation.utils.ViewUtils;
  15. import java.util.List;
  16. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  17. import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
  18. public class TopBar extends AppBarLayout {
  19. protected TitleBar titleBar;
  20. private ContextualMenu contextualMenu;
  21. protected FrameLayout titleBarAndContextualMenuContainer;
  22. protected TopTabs topTabs;
  23. public TopBar(Context context) {
  24. super(context);
  25. setId(ViewUtils.generateViewId());
  26. createLayout();
  27. }
  28. protected void createLayout() {
  29. titleBarAndContextualMenuContainer = new FrameLayout(getContext());
  30. addView(titleBarAndContextualMenuContainer);
  31. }
  32. public void addTitleBarAndSetButtons(List<TitleBarButtonParams> rightButtons,
  33. TitleBarLeftButtonParams leftButton,
  34. LeftButtonOnClickListener leftButtonOnClickListener,
  35. String navigatorEventId, boolean overrideBackPressInJs) {
  36. titleBar = createTitleBar();
  37. titleBarAndContextualMenuContainer.addView(titleBar, new ViewGroup.LayoutParams(MATCH_PARENT, MATCH_PARENT));
  38. addButtons(rightButtons, leftButton, leftButtonOnClickListener, navigatorEventId, overrideBackPressInJs);
  39. }
  40. protected TitleBar createTitleBar() {
  41. return new TitleBar(getContext());
  42. }
  43. private void addButtons(List<TitleBarButtonParams> rightButtons, TitleBarLeftButtonParams leftButton, LeftButtonOnClickListener leftButtonOnClickListener, String navigatorEventId, boolean overrideBackPressInJs) {
  44. titleBar.setRightButtons(rightButtons, navigatorEventId);
  45. titleBar.setLeftButton(leftButton, leftButtonOnClickListener, navigatorEventId, overrideBackPressInJs);
  46. }
  47. public void setTitle(String title) {
  48. titleBar.setTitle(title);
  49. }
  50. public void setSubtitle(String subtitle) {
  51. titleBar.setSubtitle(subtitle);
  52. }
  53. public void setStyle(StyleParams styleParams) {
  54. if (styleParams.topBarColor.hasColor()) {
  55. setBackgroundColor(styleParams.topBarColor.getColor());
  56. }
  57. if (styleParams.topBarTransparent) {
  58. setTransparent();
  59. }
  60. setVisibility(styleParams.topBarHidden ? GONE : VISIBLE);
  61. titleBar.setStyle(styleParams);
  62. setTopTabsStyle(styleParams);
  63. if (!styleParams.topBarElevationShadowEnabled) {
  64. disableElevationShadow();
  65. }
  66. }
  67. private void setTransparent() {
  68. setBackgroundColor(Color.TRANSPARENT);
  69. disableElevationShadow();
  70. }
  71. private void disableElevationShadow() {
  72. if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
  73. setOutlineProvider(null);
  74. }
  75. }
  76. public void setTitleBarRightButtons(String navigatorEventId, List<TitleBarButtonParams> titleBarButtons) {
  77. titleBar.setRightButtons(titleBarButtons, navigatorEventId);
  78. }
  79. public TabLayout initTabs() {
  80. topTabs = new TopTabs(getContext());
  81. addView(topTabs);
  82. return topTabs;
  83. }
  84. public void setTitleBarLeftButton(String navigatorEventId,
  85. LeftButtonOnClickListener leftButtonOnClickListener,
  86. TitleBarLeftButtonParams titleBarLeftButtonParams,
  87. boolean overrideBackPressInJs) {
  88. titleBar.setLeftButton(titleBarLeftButtonParams, leftButtonOnClickListener, navigatorEventId,
  89. overrideBackPressInJs);
  90. }
  91. private void setTopTabsStyle(StyleParams style) {
  92. if (topTabs == null) {
  93. return;
  94. }
  95. topTabs.setTopTabsTextColor(style);
  96. topTabs.setSelectedTabIndicatorStyle(style);
  97. }
  98. public void showContextualMenu(final ContextualMenuParams params, StyleParams styleParams, Callback onButtonClicked) {
  99. final ContextualMenu menuToRemove = contextualMenu != null ? contextualMenu : null;
  100. contextualMenu = new ContextualMenu(getContext(), params, styleParams, onButtonClicked);
  101. titleBarAndContextualMenuContainer.addView(contextualMenu, new ViewGroup.LayoutParams(MATCH_PARENT, WRAP_CONTENT));
  102. ViewUtils.runOnPreDraw(contextualMenu, new Runnable() {
  103. @Override
  104. public void run() {
  105. titleBar.hide();
  106. contextualMenu.show(new Runnable() {
  107. @Override
  108. public void run() {
  109. if (menuToRemove != null) {
  110. titleBarAndContextualMenuContainer.removeView(menuToRemove);
  111. }
  112. }
  113. });
  114. }
  115. });
  116. }
  117. public void onContextualMenuHidden() {
  118. contextualMenu = null;
  119. titleBar.show();
  120. }
  121. public void dismissContextualMenu() {
  122. if (contextualMenu != null) {
  123. contextualMenu.dismiss();
  124. contextualMenu = null;
  125. titleBar.show();
  126. }
  127. }
  128. }