react-native-navigation的迁移库

SideMenu.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. package com.reactnativenavigation.views;
  2. import android.content.Context;
  3. import android.support.annotation.Nullable;
  4. import android.support.v4.widget.DrawerLayout;
  5. import android.view.Gravity;
  6. import android.view.ViewGroup;
  7. import android.widget.RelativeLayout;
  8. import com.reactnativenavigation.params.SideMenuParams;
  9. import com.reactnativenavigation.screens.Screen;
  10. import com.reactnativenavigation.utils.ViewUtils;
  11. public class SideMenu extends DrawerLayout {
  12. public enum Side {
  13. Left(Gravity.LEFT), Right(Gravity.RIGHT);
  14. int gravity;
  15. Side(int gravity) {
  16. this.gravity = gravity;
  17. }
  18. public static Side fromString(String side) {
  19. return "left".equals(side.toLowerCase()) ? Left : Right;
  20. }
  21. }
  22. private ContentView leftSideMenuView;
  23. private ContentView rightSideMenuView;
  24. private RelativeLayout contentContainer;
  25. public RelativeLayout getContentContainer() {
  26. return contentContainer;
  27. }
  28. public void destroy() {
  29. destroySideMenu(leftSideMenuView);
  30. destroySideMenu(rightSideMenuView);
  31. }
  32. private void destroySideMenu(ContentView sideMenuView) {
  33. if (sideMenuView == null) {
  34. return;
  35. }
  36. sideMenuView.unmountReactView();
  37. removeView(sideMenuView);
  38. }
  39. public void setVisible(boolean visible, boolean animated, Side side) {
  40. if (!isShown() && visible) {
  41. openDrawer(animated, side);
  42. }
  43. if (isShown() && !visible) {
  44. closeDrawer(animated, side);
  45. }
  46. }
  47. public void openDrawer(Side side) {
  48. openDrawer(side.gravity);
  49. }
  50. public void openDrawer(boolean animated, Side side) {
  51. openDrawer(side.gravity, animated);
  52. }
  53. public void toggleVisible(boolean animated, Side side) {
  54. if (isDrawerOpen(side.gravity)) {
  55. closeDrawer(animated, side);
  56. } else {
  57. openDrawer(animated, side);
  58. }
  59. }
  60. public void closeDrawer(boolean animated, Side side) {
  61. closeDrawer(side.gravity, animated);
  62. }
  63. public SideMenu(Context context, SideMenuParams leftMenuParams, SideMenuParams rightMenuParams) {
  64. super(context);
  65. createContentContainer();
  66. leftSideMenuView = createSideMenu(leftMenuParams);
  67. rightSideMenuView = createSideMenu(rightMenuParams);
  68. setStyle(leftMenuParams);
  69. setStyle(rightMenuParams);
  70. }
  71. private void createContentContainer() {
  72. LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
  73. contentContainer = new RelativeLayout(getContext());
  74. contentContainer.setId(ViewUtils.generateViewId());
  75. addView(contentContainer, lp);
  76. }
  77. private ContentView createSideMenu(@Nullable SideMenuParams params) {
  78. if (params == null) {
  79. return null;
  80. }
  81. ContentView sideMenuView = new ContentView(getContext(), params.screenId, params.navigationParams);
  82. LayoutParams lp = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
  83. lp.gravity = params.side.gravity;
  84. setSideMenuWidth(sideMenuView);
  85. addView(sideMenuView, lp);
  86. return sideMenuView;
  87. }
  88. private void setSideMenuWidth(final ContentView sideMenuView) {
  89. sideMenuView.setOnDisplayListener(new Screen.OnDisplayListener() {
  90. @Override
  91. public void onDisplay() {
  92. ViewGroup.LayoutParams lp = sideMenuView.getLayoutParams();
  93. lp.width = sideMenuView.getChildAt(0).getWidth();
  94. sideMenuView.setLayoutParams(lp);
  95. }
  96. });
  97. }
  98. private void setStyle(SideMenuParams params) {
  99. if (params == null) {
  100. return;
  101. }
  102. if (params.disableOpenGesture) {
  103. setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, params.side.gravity);
  104. }
  105. }
  106. }