123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108 |
- package com.reactnativenavigation.views.collapsingToolbar;
-
- import android.content.Context;
- import android.view.View;
- import android.widget.ScrollView;
-
- import com.reactnativenavigation.params.CollapsingTopBarParams;
- import com.reactnativenavigation.utils.ViewUtils;
- import com.reactnativenavigation.views.TitleBar;
- import com.reactnativenavigation.views.TopBar;
-
- public class CollapsingTopBar extends TopBar implements CollapsingView {
- private CollapsingTopBarBackground collapsingTopBarBackground;
- private ScrollListener scrollListener;
- private float finalCollapsedTranslation;
- private CollapsingTopBarParams params;
- private final ViewCollapser viewCollapser;
-
- public CollapsingTopBar(Context context, final CollapsingTopBarParams params) {
- super(context);
- this.params = params;
- createCollapsingTopBar(params);
- calculateFinalCollapsedTranslation(params);
- viewCollapser = new ViewCollapser(this);
- }
-
- private void calculateFinalCollapsedTranslation(final CollapsingTopBarParams params) {
- ViewUtils.runOnPreDraw(this, new Runnable() {
- @Override
- public void run() {
- if (params.hasBackgroundImage()) {
- finalCollapsedTranslation =
- getCollapsingTopBarBackground().getCollapsedTopBarHeight() - getHeight();
- } else {
- finalCollapsedTranslation = -titleBar.getHeight();
- }
- }
- });
- }
-
- public void setScrollListener(ScrollListener scrollListener) {
- this.scrollListener = scrollListener;
- }
-
- private void createCollapsingTopBar(CollapsingTopBarParams params) {
- if (params.hasBackgroundImage()) {
- collapsingTopBarBackground = new CollapsingTopBarBackground(getContext(), params);
- LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, (int) CollapsingTopBarBackground.MAX_HEIGHT);
- titleBarAndContextualMenuContainer.addView(collapsingTopBarBackground, lp);
- }
- }
-
- @Override
- protected TitleBar createTitleBar() {
- if (params.hasBackgroundImage()) {
- return new CollapsingTitleBar(getContext(),
- collapsingTopBarBackground.getCollapsedTopBarHeight(),
- scrollListener);
- } else {
- return super.createTitleBar();
- }
- }
-
- public CollapsingTopBarBackground getCollapsingTopBarBackground() {
- return collapsingTopBarBackground;
- }
-
- @Override
- public void collapse(CollapseAmount amount) {
- viewCollapser.collapse(amount);
- if (titleBar instanceof CollapsingTitleBar) {
- ((CollapsingTitleBar) titleBar).collapse(amount.get());
- }
- if (collapsingTopBarBackground != null) {
- collapsingTopBarBackground.collapse(amount.get());
- }
- }
-
- public void onScrollViewAdded(ScrollView scrollView) {
- scrollListener.onScrollViewAdded(scrollView);
- }
-
- @Override
- public float getFinalCollapseValue() {
- return finalCollapsedTranslation;
- }
-
- public int getCollapsedHeight() {
- if (params.hasBackgroundImage()) {
- return collapsingTopBarBackground.getCollapsedTopBarHeight();
- } else if (topTabs != null) {
- return topTabs.getHeight();
- } else {
- return titleBar.getHeight();
- }
- }
-
- @Override
- public float getCurrentCollapseValue() {
- return getTranslationY();
- }
-
- @Override
- public View asView() {
- return this;
- }
- }
|