react-native-navigation的迁移库

CollapsingViewPagerScreen.java 5.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package com.reactnativenavigation.screens;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.support.v4.view.ViewPager;
  5. import android.support.v7.app.AppCompatActivity;
  6. import android.widget.ScrollView;
  7. import com.reactnativenavigation.events.Event;
  8. import com.reactnativenavigation.events.ViewPagerScreenChangedEvent;
  9. import com.reactnativenavigation.events.ViewPagerScreenScrollStartEvent;
  10. import com.reactnativenavigation.params.PageParams;
  11. import com.reactnativenavigation.params.ScreenParams;
  12. import com.reactnativenavigation.views.CollapsingContentView;
  13. import com.reactnativenavigation.views.ContentView;
  14. import com.reactnativenavigation.views.LeftButtonOnClickListener;
  15. import com.reactnativenavigation.views.collapsingToolbar.CollapseAmount;
  16. import com.reactnativenavigation.views.collapsingToolbar.CollapseCalculator;
  17. import com.reactnativenavigation.views.collapsingToolbar.CollapsingTopBar;
  18. import com.reactnativenavigation.views.collapsingToolbar.CollapsingView;
  19. import com.reactnativenavigation.views.collapsingToolbar.CollapsingViewMeasurer;
  20. import com.reactnativenavigation.views.collapsingToolbar.CollapsingViewPager;
  21. import com.reactnativenavigation.views.collapsingToolbar.CollapsingViewPagerContentViewMeasurer;
  22. import com.reactnativenavigation.views.collapsingToolbar.OnScrollListener;
  23. import com.reactnativenavigation.views.collapsingToolbar.OnScrollViewAddedListener;
  24. import com.reactnativenavigation.views.collapsingToolbar.ScrollListener;
  25. import com.reactnativenavigation.views.collapsingToolbar.behaviours.CollapseBehaviour;
  26. @SuppressLint("ViewConstructor")
  27. public class CollapsingViewPagerScreen extends ViewPagerScreen {
  28. public CollapsingViewPagerScreen(AppCompatActivity activity, ScreenParams screenParams, LeftButtonOnClickListener backButtonListener) {
  29. super(activity, screenParams, backButtonListener);
  30. }
  31. @Override
  32. protected void createTopBar() {
  33. final CollapsingTopBar topBar = new CollapsingTopBar(getContext(), styleParams.collapsingTopBarParams);
  34. topBar.setScrollListener(getScrollListener());
  35. this.topBar = topBar;
  36. }
  37. @Override
  38. protected ViewPager createViewPager(Context context) {
  39. CollapsingViewPager viewPager = new CollapsingViewPager(context);
  40. if (screenParams.styleParams.drawScreenBelowTopBar) {
  41. viewPager.setViewMeasurer(new CollapsingViewMeasurer((CollapsingTopBar) topBar, this));
  42. }
  43. return viewPager;
  44. }
  45. protected ContentView createContentView(PageParams tab) {
  46. if (tab.hasCollapsingTopBar()) {
  47. CollapsingContentView contentView = new CollapsingContentView(getContext(), tab.screenId, tab.navigationParams);
  48. contentView.setViewMeasurer(new CollapsingViewPagerContentViewMeasurer((CollapsingTopBar) topBar, this, screenParams.styleParams));
  49. setupCollapseDetection(contentView);
  50. return contentView;
  51. } else {
  52. return new ContentView(getContext(), tab.screenId, tab.navigationParams);
  53. }
  54. }
  55. private void setupCollapseDetection(CollapsingContentView contentView) {
  56. contentView.setupCollapseDetection(getScrollListener(), new OnScrollViewAddedListener() {
  57. @Override
  58. public void onScrollViewAdded(ScrollView scrollView) {
  59. ((CollapsingTopBar) topBar).onScrollViewAdded(scrollView);
  60. }
  61. });
  62. }
  63. private ScrollListener getScrollListener() {
  64. return new ScrollListener(new CollapseCalculator((CollapsingView) topBar, getCollapseBehaviour()),
  65. new OnScrollListener() {
  66. @Override
  67. public void onScroll(CollapseAmount amount) {
  68. ((CollapsingView) topBar).collapse(amount);
  69. ((CollapsingView) viewPager).collapse(amount);
  70. }
  71. @Override
  72. public void onFling(CollapseAmount amount) {
  73. ((CollapsingView) topBar).collapse(amount);
  74. ((CollapsingView) viewPager).collapse(amount);
  75. }
  76. },
  77. getCollapseBehaviour()
  78. );
  79. }
  80. private CollapseBehaviour getCollapseBehaviour() {
  81. return screenParams.styleParams.collapsingTopBarParams.collapseBehaviour;
  82. }
  83. @Override
  84. public void onEvent(Event event) {
  85. super.onEvent(event);
  86. if (ViewPagerScreenScrollStartEvent.TYPE.equals(event.getType()) || ViewPagerScreenChangedEvent.TYPE.equals(event.getType())) {
  87. ((CollapsingView) topBar).collapse(new CollapseAmount(CollapseCalculator.Direction.Down));
  88. ((CollapsingView) viewPager).collapse(new CollapseAmount(CollapseCalculator.Direction.Down));
  89. }
  90. }
  91. @Override
  92. public void destroy() {
  93. super.destroy();
  94. for (ContentView contentView : contentViews) {
  95. if (contentView instanceof CollapsingContentView) {
  96. ((CollapsingContentView) contentView).destroy();
  97. }
  98. }
  99. }
  100. }