react-native-navigation的迁移库

FragmentScreen.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. package com.reactnativenavigation.screens;
  2. import android.app.Fragment;
  3. import android.app.FragmentManager;
  4. import android.app.FragmentTransaction;
  5. import android.os.Bundle;
  6. import android.support.annotation.Nullable;
  7. import android.support.v7.app.AppCompatActivity;
  8. import android.widget.FrameLayout;
  9. import com.reactnativenavigation.R;
  10. import com.reactnativenavigation.params.ScreenParams;
  11. import com.reactnativenavigation.utils.ViewUtils;
  12. import com.reactnativenavigation.views.ContentView;
  13. import com.reactnativenavigation.views.LeftButtonOnClickListener;
  14. import java.lang.reflect.InvocationTargetException;
  15. import java.lang.reflect.Method;
  16. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  17. @SuppressWarnings("ResourceType")
  18. public class FragmentScreen extends Screen {
  19. private static final String CONTRACT_GET_FRAGMENT = "getFragment";
  20. private static final String CONTRACT_GET_SUPPORT_FRAGMENT = "getSupportFragment";
  21. private FrameLayout content;
  22. private ContentView contentView;
  23. public FragmentScreen(AppCompatActivity activity, ScreenParams screenParams, LeftButtonOnClickListener leftButtonOnClickListener) {
  24. super(activity, screenParams, leftButtonOnClickListener);
  25. }
  26. @Override
  27. protected void createContent() {
  28. content = new FrameLayout(getContext());
  29. content.setId(R.id.fragment_screen_content);
  30. addContent();
  31. addFragment();
  32. }
  33. private void addContent() {
  34. contentView = new ContentView(getContext(),
  35. screenParams.screenId,
  36. screenParams.navigationParams);
  37. addView(contentView, 0, 0);
  38. LayoutParams params = new LayoutParams(MATCH_PARENT, MATCH_PARENT);
  39. if (screenParams.styleParams.drawScreenBelowTopBar) {
  40. params.addRule(BELOW, topBar.getId());
  41. }
  42. addView(content, params);
  43. }
  44. private void addFragment() {
  45. try {
  46. Fragment fragment = tryGetFragment();
  47. if (fragment != null) {
  48. addFragment(fragment);
  49. return;
  50. }
  51. android.support.v4.app.Fragment supportFragment = tryGetSupportFragment();
  52. if (supportFragment != null) {
  53. addSupportFragment(supportFragment);
  54. return;
  55. }
  56. throw new RuntimeException("must provide public static method " + CONTRACT_GET_FRAGMENT + " or " + CONTRACT_GET_SUPPORT_FRAGMENT);
  57. } catch (Exception e) {
  58. throw new RuntimeException(e);
  59. }
  60. }
  61. private void addFragment(Fragment fragment) {
  62. FragmentManager fm = activity.getFragmentManager();
  63. FragmentTransaction transaction = fm.beginTransaction();
  64. transaction.add(R.id.fragment_screen_content, fragment);
  65. transaction.commit();
  66. }
  67. private void addSupportFragment(android.support.v4.app.Fragment supportFragment) {
  68. android.support.v4.app.FragmentManager fm = activity.getSupportFragmentManager();
  69. android.support.v4.app.FragmentTransaction transaction = fm.beginTransaction();
  70. transaction.add(R.id.fragment_screen_content, supportFragment);
  71. transaction.commit();
  72. }
  73. @Nullable
  74. private Fragment tryGetFragment() throws ClassNotFoundException, IllegalAccessException, InvocationTargetException {
  75. try {
  76. String className = screenParams.fragmentCreatorClassName;
  77. Class<?> fragmentCreatorClass = Class.forName(className);
  78. Method method = fragmentCreatorClass.getMethod(CONTRACT_GET_FRAGMENT, Bundle.class);
  79. return (android.app.Fragment) method.invoke(null, screenParams.fragmentCreatorPassProps);
  80. } catch (NoSuchMethodException noSuchMethod) {
  81. return null;
  82. }
  83. }
  84. @Nullable
  85. private android.support.v4.app.Fragment tryGetSupportFragment() throws ClassNotFoundException, IllegalAccessException, InvocationTargetException {
  86. try {
  87. String className = screenParams.fragmentCreatorClassName;
  88. Class<?> fragmentCreatorClass = Class.forName(className);
  89. Method method = fragmentCreatorClass.getMethod(CONTRACT_GET_SUPPORT_FRAGMENT, Bundle.class);
  90. return (android.support.v4.app.Fragment) method.invoke(null, screenParams.fragmentCreatorPassProps);
  91. } catch (NoSuchMethodException noSuchMethod) {
  92. return null;
  93. }
  94. }
  95. @Override
  96. public void unmountReactView() {
  97. contentView.unmountReactView();
  98. }
  99. @Override
  100. public String getNavigatorEventId() {
  101. return screenParams.getNavigatorEventId();
  102. }
  103. @Override
  104. public void setOnDisplayListener(final OnDisplayListener onContentViewDisplayedListener) {
  105. ViewUtils.runOnPreDraw(content, new Runnable() {
  106. @Override
  107. public void run() {
  108. onContentViewDisplayedListener.onDisplay();
  109. }
  110. });
  111. }
  112. }