react-native-navigation的迁移库

ContentView.java 2.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.reactnativenavigation.views;
  2. import android.content.Context;
  3. import android.view.View;
  4. import com.facebook.react.ReactRootView;
  5. import com.reactnativenavigation.NavigationApplication;
  6. import com.reactnativenavigation.params.NavigationParams;
  7. import com.reactnativenavigation.screens.SingleScreen;
  8. import com.reactnativenavigation.utils.ViewUtils;
  9. import com.reactnativenavigation.views.utils.ViewMeasurer;
  10. public class ContentView extends ReactRootView {
  11. private final String screenId;
  12. private final NavigationParams navigationParams;
  13. boolean isContentVisible = false;
  14. private SingleScreen.OnDisplayListener onDisplayListener;
  15. protected ViewMeasurer viewMeasurer;
  16. public void setOnDisplayListener(SingleScreen.OnDisplayListener onDisplayListener) {
  17. this.onDisplayListener = onDisplayListener;
  18. }
  19. public ContentView(Context context, String screenId, NavigationParams navigationParams) {
  20. super(context);
  21. this.screenId = screenId;
  22. this.navigationParams = navigationParams;
  23. attachToJS();
  24. viewMeasurer = new ViewMeasurer();
  25. }
  26. public void setViewMeasurer(ViewMeasurer viewMeasurer) {
  27. this.viewMeasurer = viewMeasurer;
  28. }
  29. private void attachToJS() {
  30. startReactApplication(NavigationApplication.instance.getReactGateway().getReactInstanceManager(), screenId,
  31. navigationParams.toBundle());
  32. }
  33. public String getNavigatorEventId() {
  34. return navigationParams.navigatorEventId;
  35. }
  36. public void unmountReactView() {
  37. unmountReactApplication();
  38. }
  39. @Override
  40. protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  41. super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  42. int measuredHeight = viewMeasurer.getMeasuredHeight(heightMeasureSpec);
  43. setMeasuredDimension(viewMeasurer.getMeasuredWidth(widthMeasureSpec),
  44. measuredHeight);
  45. }
  46. @Override
  47. public void onViewAdded(final View child) {
  48. super.onViewAdded(child);
  49. detectContentViewVisible(child);
  50. }
  51. private void detectContentViewVisible(View child) {
  52. if (onDisplayListener != null) {
  53. ViewUtils.runOnPreDraw(child, new Runnable() {
  54. @Override
  55. public void run() {
  56. if (!isContentVisible) {
  57. isContentVisible = true;
  58. onDisplayListener.onDisplay();
  59. onDisplayListener = null;
  60. }
  61. }
  62. });
  63. }
  64. }
  65. }