react-native-navigation的迁移库

CollapsingTopBar.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. package com.reactnativenavigation.views.collapsingToolbar;
  2. import android.content.Context;
  3. import android.view.View;
  4. import android.widget.ScrollView;
  5. import com.reactnativenavigation.params.CollapsingTopBarParams;
  6. import com.reactnativenavigation.utils.ViewUtils;
  7. import com.reactnativenavigation.views.TitleBar;
  8. import com.reactnativenavigation.views.TopBar;
  9. public class CollapsingTopBar extends TopBar implements CollapsingView {
  10. private CollapsingTopBarBackground collapsingTopBarBackground;
  11. private ScrollListener scrollListener;
  12. private float finalCollapsedTranslation;
  13. private CollapsingTopBarParams params;
  14. private final ViewCollapser viewCollapser;
  15. public CollapsingTopBar(Context context, final CollapsingTopBarParams params) {
  16. super(context);
  17. this.params = params;
  18. createCollapsingTopBar(params);
  19. calculateFinalCollapsedTranslation(params);
  20. viewCollapser = new ViewCollapser(this);
  21. }
  22. private void calculateFinalCollapsedTranslation(final CollapsingTopBarParams params) {
  23. ViewUtils.runOnPreDraw(this, new Runnable() {
  24. @Override
  25. public void run() {
  26. if (params.hasBackgroundImage()) {
  27. finalCollapsedTranslation =
  28. getCollapsingTopBarBackground().getCollapsedTopBarHeight() - getHeight();
  29. } else {
  30. finalCollapsedTranslation = -titleBar.getHeight();
  31. }
  32. }
  33. });
  34. }
  35. public void setScrollListener(ScrollListener scrollListener) {
  36. this.scrollListener = scrollListener;
  37. }
  38. private void createCollapsingTopBar(CollapsingTopBarParams params) {
  39. if (params.hasBackgroundImage()) {
  40. collapsingTopBarBackground = new CollapsingTopBarBackground(getContext(), params);
  41. LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, (int) CollapsingTopBarBackground.MAX_HEIGHT);
  42. titleBarAndContextualMenuContainer.addView(collapsingTopBarBackground, lp);
  43. }
  44. }
  45. @Override
  46. protected TitleBar createTitleBar() {
  47. if (params.hasBackgroundImage()) {
  48. return new CollapsingTitleBar(getContext(),
  49. collapsingTopBarBackground.getCollapsedTopBarHeight(),
  50. scrollListener);
  51. } else {
  52. return super.createTitleBar();
  53. }
  54. }
  55. public CollapsingTopBarBackground getCollapsingTopBarBackground() {
  56. return collapsingTopBarBackground;
  57. }
  58. @Override
  59. public void collapse(CollapseAmount amount) {
  60. viewCollapser.collapse(amount);
  61. if (titleBar instanceof CollapsingTitleBar) {
  62. ((CollapsingTitleBar) titleBar).collapse(amount.get());
  63. }
  64. if (collapsingTopBarBackground != null) {
  65. collapsingTopBarBackground.collapse(amount.get());
  66. }
  67. }
  68. public void onScrollViewAdded(ScrollView scrollView) {
  69. scrollListener.onScrollViewAdded(scrollView);
  70. }
  71. @Override
  72. public float getFinalCollapseValue() {
  73. return finalCollapsedTranslation;
  74. }
  75. public int getCollapsedHeight() {
  76. if (params.hasBackgroundImage()) {
  77. return collapsingTopBarBackground.getCollapsedTopBarHeight();
  78. } else if (topTabs != null) {
  79. return topTabs.getHeight();
  80. } else {
  81. return titleBar.getHeight();
  82. }
  83. }
  84. @Override
  85. public float getCurrentCollapseValue() {
  86. return getTranslationY();
  87. }
  88. @Override
  89. public View asView() {
  90. return this;
  91. }
  92. }