react-native-navigation的迁移库

ComponentLayout.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. package com.reactnativenavigation.views;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.view.MotionEvent;
  5. import android.view.View;
  6. import android.widget.FrameLayout;
  7. import android.widget.RelativeLayout;
  8. import com.reactnativenavigation.interfaces.ScrollEventListener;
  9. import com.reactnativenavigation.parse.Options;
  10. import com.reactnativenavigation.utils.UiUtils;
  11. import com.reactnativenavigation.utils.ViewUtils;
  12. import com.reactnativenavigation.viewcontrollers.IReactView;
  13. import com.reactnativenavigation.viewcontrollers.TopBarButtonController;
  14. import com.reactnativenavigation.views.topbar.TopBar;
  15. import com.reactnativenavigation.views.touch.OverlayTouchDelegate;
  16. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  17. @SuppressLint("ViewConstructor")
  18. public class ComponentLayout extends FrameLayout implements ReactComponent, TopBarButtonController.OnClickListener {
  19. private IReactView reactView;
  20. private final OverlayTouchDelegate touchDelegate;
  21. public ComponentLayout(Context context, IReactView reactView) {
  22. super(context);
  23. this.reactView = reactView;
  24. addView(reactView.asView(), MATCH_PARENT, MATCH_PARENT);
  25. setContentDescription("ComponentLayout");
  26. touchDelegate = new OverlayTouchDelegate(reactView);
  27. }
  28. @Override
  29. public boolean isReady() {
  30. return reactView.isReady();
  31. }
  32. @Override
  33. public View asView() {
  34. return this;
  35. }
  36. @Override
  37. public void destroy() {
  38. reactView.destroy();
  39. }
  40. @Override
  41. public void sendComponentStart() {
  42. reactView.sendComponentStart();
  43. }
  44. @Override
  45. public void sendComponentStop() {
  46. reactView.sendComponentStop();
  47. }
  48. public void applyOptions(Options options) {
  49. touchDelegate.setInterceptTouchOutside(options.overlayOptions.interceptTouchOutside);
  50. }
  51. @Override
  52. public void sendOnNavigationButtonPressed(String buttonId) {
  53. reactView.sendOnNavigationButtonPressed(buttonId);
  54. }
  55. @Override
  56. public ScrollEventListener getScrollEventListener() {
  57. return reactView.getScrollEventListener();
  58. }
  59. @Override
  60. public void dispatchTouchEventToJs(MotionEvent event) {
  61. reactView.dispatchTouchEventToJs(event);
  62. }
  63. @Override
  64. public void drawBehindTopBar() {
  65. if (getLayoutParams() instanceof RelativeLayout.LayoutParams) {
  66. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
  67. layoutParams.topMargin = 0;
  68. setLayoutParams(layoutParams);
  69. }
  70. }
  71. @Override
  72. public void drawBelowTopBar(TopBar topBar) {
  73. if (getLayoutParams() instanceof RelativeLayout.LayoutParams) {
  74. RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) getLayoutParams();
  75. int topBarHeight = ViewUtils.getPreferredHeight(topBar);
  76. if (topBarHeight == 0) {
  77. UiUtils.runOnPreDrawOnce(topBar, () -> layoutParams.topMargin = topBar.getHeight());
  78. } else {
  79. layoutParams.topMargin = topBarHeight;
  80. }
  81. setLayoutParams(layoutParams);
  82. }
  83. }
  84. @Override
  85. public boolean isRendered() {
  86. return reactView.isRendered();
  87. }
  88. @Override
  89. public void onPress(String buttonId) {
  90. reactView.sendOnNavigationButtonPressed(buttonId);
  91. }
  92. @Override
  93. public boolean onInterceptTouchEvent(MotionEvent ev) {
  94. return touchDelegate.onInterceptTouchEvent(ev);
  95. }
  96. }