react-native-navigation的迁移库

TopBar.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. package com.reactnativenavigation.views.topbar;
  2. import android.annotation.SuppressLint;
  3. import android.content.Context;
  4. import android.graphics.Typeface;
  5. import android.os.Build;
  6. import android.support.annotation.ColorInt;
  7. import android.support.annotation.NonNull;
  8. import android.support.annotation.RestrictTo;
  9. import android.support.annotation.VisibleForTesting;
  10. import android.support.design.widget.AppBarLayout;
  11. import android.support.v4.view.ViewPager;
  12. import android.support.v7.widget.Toolbar;
  13. import android.view.Gravity;
  14. import android.view.View;
  15. import android.view.ViewGroup;
  16. import android.widget.FrameLayout;
  17. import android.widget.LinearLayout;
  18. import android.widget.RelativeLayout;
  19. import android.widget.TextView;
  20. import com.reactnativenavigation.BuildConfig;
  21. import com.reactnativenavigation.anim.TopBarAnimator;
  22. import com.reactnativenavigation.anim.TopBarCollapseBehavior;
  23. import com.reactnativenavigation.interfaces.ScrollEventListener;
  24. import com.reactnativenavigation.parse.Alignment;
  25. import com.reactnativenavigation.parse.AnimationOptions;
  26. import com.reactnativenavigation.parse.BackButton;
  27. import com.reactnativenavigation.parse.Component;
  28. import com.reactnativenavigation.parse.params.Button;
  29. import com.reactnativenavigation.parse.params.Color;
  30. import com.reactnativenavigation.parse.params.Fraction;
  31. import com.reactnativenavigation.parse.params.Number;
  32. import com.reactnativenavigation.utils.CompatUtils;
  33. import com.reactnativenavigation.utils.ImageLoader;
  34. import com.reactnativenavigation.utils.UiUtils;
  35. import com.reactnativenavigation.utils.ViewUtils;
  36. import com.reactnativenavigation.viewcontrollers.ReactViewCreator;
  37. import com.reactnativenavigation.viewcontrollers.TopBarButtonController;
  38. import com.reactnativenavigation.viewcontrollers.topbar.TopBarBackgroundViewController;
  39. import com.reactnativenavigation.views.StackLayout;
  40. import com.reactnativenavigation.views.titlebar.TitleBar;
  41. import com.reactnativenavigation.views.titlebar.TitleBarReactViewCreator;
  42. import com.reactnativenavigation.views.toptabs.TopTabs;
  43. import java.util.List;
  44. import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
  45. import static android.view.ViewGroup.LayoutParams.WRAP_CONTENT;
  46. @SuppressLint("ViewConstructor")
  47. public class TopBar extends AppBarLayout implements ScrollEventListener.ScrollAwareView {
  48. private TitleBar titleBar;
  49. private final TopBarCollapseBehavior collapsingBehavior;
  50. private TopBarAnimator animator;
  51. private TopTabs topTabs;
  52. private FrameLayout root;
  53. private LinearLayout content;
  54. private StackLayout parentView;
  55. private TopBarBackgroundViewController topBarBackgroundViewController;
  56. private View border;
  57. private ImageLoader imageLoader;
  58. public TopBar(final Context context, ReactViewCreator buttonCreator, TitleBarReactViewCreator titleBarReactViewCreator, TopBarBackgroundViewController topBarBackgroundViewController, TopBarButtonController.OnClickListener onClickListener, StackLayout parentView, ImageLoader imageLoader) {
  59. super(context);
  60. this.imageLoader = imageLoader;
  61. collapsingBehavior = new TopBarCollapseBehavior(this);
  62. this.topBarBackgroundViewController = topBarBackgroundViewController;
  63. this.parentView = parentView;
  64. topTabs = new TopTabs(getContext());
  65. animator = new TopBarAnimator(this, parentView.getStackId());
  66. createLayout(buttonCreator, titleBarReactViewCreator, onClickListener);
  67. }
  68. private void createLayout(ReactViewCreator buttonCreator, TitleBarReactViewCreator titleBarReactViewCreator, TopBarButtonController.OnClickListener onClickListener) {
  69. setId(CompatUtils.generateViewId());
  70. titleBar = createTitleBar(getContext(), buttonCreator, titleBarReactViewCreator, onClickListener, imageLoader);
  71. topTabs = createTopTabs();
  72. border = createBorder();
  73. content = createContentLayout();
  74. root = new FrameLayout(getContext());
  75. root.setId(CompatUtils.generateViewId());
  76. content.addView(titleBar);
  77. content.addView(topTabs);
  78. root.addView(content);
  79. root.addView(border);
  80. addView(root, MATCH_PARENT, WRAP_CONTENT);
  81. if (BuildConfig.DEBUG) setContentDescription("TopBar");
  82. }
  83. private LinearLayout createContentLayout() {
  84. LinearLayout content = new LinearLayout(getContext());
  85. content.setOrientation(VERTICAL);
  86. return content;
  87. }
  88. @NonNull
  89. private TopTabs createTopTabs() {
  90. RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, WRAP_CONTENT);
  91. lp.addRule(RelativeLayout.BELOW, titleBar.getId());
  92. TopTabs topTabs = new TopTabs(getContext());
  93. topTabs.setLayoutParams(lp);
  94. topTabs.setVisibility(GONE);
  95. return topTabs;
  96. }
  97. private View createBorder() {
  98. View border = new View(getContext());
  99. border.setBackgroundColor(android.graphics.Color.TRANSPARENT);
  100. FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(MATCH_PARENT, 0);
  101. lp.gravity = Gravity.BOTTOM;
  102. border.setLayoutParams(lp);
  103. return border;
  104. }
  105. protected TitleBar createTitleBar(Context context, ReactViewCreator buttonCreator, TitleBarReactViewCreator reactViewCreator, TopBarButtonController.OnClickListener onClickListener, ImageLoader imageLoader) {
  106. TitleBar titleBar = new TitleBar(context, buttonCreator, reactViewCreator, onClickListener, imageLoader);
  107. titleBar.setId(CompatUtils.generateViewId());
  108. return titleBar;
  109. }
  110. public void setHeight(int height) {
  111. if (height == getLayoutParams().height) return;
  112. ViewGroup.LayoutParams lp = getLayoutParams();
  113. lp.height = (int) UiUtils.dpToPx(getContext(), height);
  114. setLayoutParams(lp);
  115. }
  116. public void setTitleHeight(int height) {
  117. titleBar.setHeight(height);
  118. }
  119. public void setTitle(String title) {
  120. titleBar.setTitle(title);
  121. }
  122. public String getTitle() {
  123. return titleBar.getTitle();
  124. }
  125. public void setSubtitle(String subtitle) {
  126. titleBar.setSubtitle(subtitle);
  127. }
  128. public void setSubtitleColor(@ColorInt int color) {
  129. titleBar.setSubtitleTextColor(color);
  130. }
  131. public void setSubtitleFontFamily(Typeface fontFamily) {
  132. titleBar.setSubtitleTypeface(fontFamily);
  133. }
  134. public void setSubtitleFontSize(double size) {
  135. titleBar.setSubtitleFontSize(size);
  136. }
  137. public void setSubtitleAlignment(Alignment alignment) {
  138. titleBar.setSubtitleAlignment(alignment);
  139. }
  140. public void setTestId(String testId) {
  141. setTag(testId);
  142. }
  143. public void setTitleTextColor(@ColorInt int color) {
  144. titleBar.setTitleTextColor(color);
  145. }
  146. public void setTitleFontSize(double size) {
  147. titleBar.setTitleFontSize(size);
  148. }
  149. public void setTitleTypeface(Typeface typeface) {
  150. titleBar.setTitleTypeface(typeface);
  151. }
  152. public void setTitleAlignment(Alignment alignment) {
  153. titleBar.setTitleAlignment(alignment);
  154. }
  155. public void setTitleComponent(Component component) {
  156. titleBar.setComponent(component);
  157. }
  158. public void setBackgroundComponent(Component component) {
  159. if (component.hasValue()) {
  160. topBarBackgroundViewController.setComponent(component);
  161. RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(MATCH_PARENT, ViewUtils.getPreferredHeight(this));
  162. root.addView(topBarBackgroundViewController.getView(), 0, lp);
  163. }
  164. }
  165. public void setTopTabFontFamily(int tabIndex, Typeface fontFamily) {
  166. topTabs.setFontFamily(tabIndex, fontFamily);
  167. }
  168. public void applyTopTabsColors(Color selectedTabColor, Color unselectedTabColor) {
  169. topTabs.applyTopTabsColors(selectedTabColor, unselectedTabColor);
  170. }
  171. public void applyTopTabsFontSize(Number fontSize) {
  172. topTabs.applyTopTabsFontSize(fontSize);
  173. }
  174. public void setTopTabsVisible(boolean visible) {
  175. topTabs.setVisibility(this, visible);
  176. }
  177. public void setTopTabsHeight(int height) {
  178. if (topTabs.getLayoutParams().height == height) return;
  179. topTabs.getLayoutParams().height = height > 0 ? (int) UiUtils.dpToPx(getContext(), height) : height;
  180. topTabs.setLayoutParams(topTabs.getLayoutParams());
  181. }
  182. public void setBackButton(BackButton backButton) {
  183. titleBar.setBackButton(backButton);
  184. }
  185. public void setLeftButtons(List<Button> leftButtons) {
  186. titleBar.setLeftButtons(leftButtons);
  187. }
  188. public void setRightButtons(List<Button> rightButtons) {
  189. titleBar.setRightButtons(rightButtons);
  190. }
  191. public void setElevation(Fraction elevation) {
  192. if (elevation.hasValue() &&
  193. Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
  194. getElevation() != elevation.get().floatValue()) {
  195. setElevation(UiUtils.dpToPx(getContext(), elevation.get().floatValue()));
  196. }
  197. }
  198. public Toolbar getTitleBar() {
  199. return titleBar;
  200. }
  201. public void initTopTabs(ViewPager viewPager) {
  202. topTabs.setVisibility(VISIBLE);
  203. topTabs.init(viewPager);
  204. }
  205. public void enableCollapse(ScrollEventListener scrollEventListener) {
  206. collapsingBehavior.enableCollapse(scrollEventListener);
  207. }
  208. public void disableCollapse() {
  209. collapsingBehavior.disableCollapse();
  210. }
  211. public void show() {
  212. if (visible() || animator.isAnimatingShow()) return;
  213. resetAnimationOptions();
  214. setVisibility(View.VISIBLE);
  215. }
  216. private boolean visible() {
  217. return getVisibility() == View.VISIBLE;
  218. }
  219. public void showAnimate(AnimationOptions options) {
  220. if (visible() || animator.isAnimatingShow()) return;
  221. animator.show(options);
  222. }
  223. public void hide() {
  224. if (!animator.isAnimatingHide()) {
  225. setVisibility(View.GONE);
  226. }
  227. }
  228. public void hideAnimate(AnimationOptions options) {
  229. hideAnimate(options, () -> {});
  230. }
  231. public void hideAnimate(AnimationOptions options, Runnable onAnimationEnd) {
  232. animator.hide(options, onAnimationEnd);
  233. }
  234. @Override
  235. public void setVisibility(int visibility) {
  236. super.setVisibility(visibility);
  237. if (visibility == View.GONE) {
  238. this.parentView.removeView(this);
  239. } else if (visibility == View.VISIBLE && this.getParent() == null) {
  240. this.parentView.addView(this);
  241. }
  242. }
  243. public void clear() {
  244. topBarBackgroundViewController.destroy();
  245. topBarBackgroundViewController = new TopBarBackgroundViewController(topBarBackgroundViewController);
  246. titleBar.clear();
  247. }
  248. public void clearTopTabs() {
  249. topTabs.clear(this);
  250. }
  251. @VisibleForTesting
  252. public TopTabs getTopTabs() {
  253. return topTabs;
  254. }
  255. @VisibleForTesting
  256. public void setAnimator(TopBarAnimator animator) {
  257. this.animator = animator;
  258. }
  259. @RestrictTo(RestrictTo.Scope.TESTS)
  260. public TextView getTitleTextView() {
  261. return titleBar.findTitleTextView();
  262. }
  263. public void resetAnimationOptions() {
  264. setTranslationY(0);
  265. setTranslationX(0);
  266. setAlpha(1);
  267. setScaleY(1);
  268. setScaleX(1);
  269. setRotationX(0);
  270. setRotationY(0);
  271. setRotation(0);
  272. }
  273. public void setBorderHeight(double height) {
  274. border.getLayoutParams().height = (int) UiUtils.dpToPx(getContext(), (float) height);
  275. }
  276. public void setBorderColor(int color) {
  277. border.setBackgroundColor(color);
  278. }
  279. }