Java Code Examples for androidx.core.view.ViewCompat#getMinimumHeight()

The following examples show how to use androidx.core.view.ViewCompat#getMinimumHeight() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: SubtitleCollapsingToolbarLayout.java    From collapsingtoolbarlayout-subtitle with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the amount of visible height in pixels used to define when to trigger a scrim
 * visibility change.
 *
 * @see #setScrimVisibleHeightTrigger(int)
 */
public int getScrimVisibleHeightTrigger() {
    if (scrimVisibleHeightTrigger >= 0) {
        // If we have one explicitly set, return it
        return scrimVisibleHeightTrigger;
    }

    // Otherwise we'll use the default computed value
    final int insetTop = lastInsets != null ? lastInsets.getSystemWindowInsetTop() : 0;

    final int minHeight = ViewCompat.getMinimumHeight(this);
    if (minHeight > 0) {
        // If we have a minHeight set, lets use 2 * minHeight (capped at our height)
        return Math.min((minHeight * 2) + insetTop, getHeight());
    }

    // If we reach here then we don't have a min height set. Instead we'll take a
    // guess at 1/3 of our height being visible
    return getHeight() / 3;
}
 
Example 2
Source File: CollapsingTitleBarLayout.java    From UIWidget with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the amount of visible height in pixels used to define when to trigger a scrim
 * visibility change.
 *
 * @see #setScrimVisibleHeightTrigger(int)
 */
public int getScrimVisibleHeightTrigger() {
    if (mScrimVisibleHeightTrigger >= 0) {
        // If we have one explicitly set, return it
        return mScrimVisibleHeightTrigger;
    }

    // Otherwise we'll use the default computed value
    final int insetTop = mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0;

    final int minHeight = ViewCompat.getMinimumHeight(this);
    if (minHeight > 0) {
        // If we have a minHeight set, lets use 2 * minHeight (capped at our height)
        return Math.min((minHeight * 2) + insetTop, getHeight());
    }

    // If we reach here then we don't have a min height set. Instead we'll take a
    // guess at 1/3 of our height being visible
    return getHeight() / 3;
}
 
Example 3
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
public final int getMinimumHeightForVisibleOverlappingContent() {
  final int topInset = getTopInset();
  final int minHeight = ViewCompat.getMinimumHeight(this);
  if (minHeight != 0) {
    // If this layout has a min height, use it (doubled)
    return (minHeight * 2) + topInset;
  }

  // Otherwise, we'll use twice the min height of our last child
  final int childCount = getChildCount();
  final int lastChildMinHeight =
      childCount >= 1 ? ViewCompat.getMinimumHeight(getChildAt(childCount - 1)) : 0;
  if (lastChildMinHeight != 0) {
    return (lastChildMinHeight * 2) + topInset;
  }

  // If we reach here then we don't have a min height explicitly set. Instead we'll take a
  // guess at 1/3 of our height being visible
  return getHeight() / 3;
}
 
Example 4
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
public Parcelable onSaveInstanceState(@NonNull CoordinatorLayout parent, @NonNull T abl) {
  final Parcelable superState = super.onSaveInstanceState(parent, abl);
  final int offset = getTopAndBottomOffset();

  // Try and find the first visible child...
  for (int i = 0, count = abl.getChildCount(); i < count; i++) {
    View child = abl.getChildAt(i);
    final int visBottom = child.getBottom() + offset;

    if (child.getTop() + offset <= 0 && visBottom >= 0) {
      final SavedState ss = new SavedState(superState);
      ss.firstVisibleChildIndex = i;
      ss.firstVisibleChildAtMinimumHeight =
          visBottom == (ViewCompat.getMinimumHeight(child) + abl.getTopInset());
      ss.firstVisibleChildPercentageShown = visBottom / (float) child.getHeight();
      return ss;
    }
  }

  // Else we'll just return the super state
  return superState;
}
 
Example 5
Source File: CollapsingToolbarLayout.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the amount of visible height in pixels used to define when to trigger a scrim
 * visibility change.
 *
 * @see #setScrimVisibleHeightTrigger(int)
 */
public int getScrimVisibleHeightTrigger() {
  if (scrimVisibleHeightTrigger >= 0) {
    // If we have one explicitly set, return it
    return scrimVisibleHeightTrigger;
  }

  // Otherwise we'll use the default computed value
  final int insetTop = lastInsets != null ? lastInsets.getSystemWindowInsetTop() : 0;

  final int minHeight = ViewCompat.getMinimumHeight(this);
  if (minHeight > 0) {
    // If we have a minHeight set, lets use 2 * minHeight (capped at our height)
    return Math.min((minHeight * 2) + insetTop, getHeight());
  }

  // If we reach here then we don't have a min height set. Instead we'll take a
  // guess at 1/3 of our height being visible
  return getHeight() / 3;
}
 
Example 6
Source File: SubtitleCollapsingToolbarLayout.java    From collapsingtoolbarlayout-subtitle with Apache License 2.0 5 votes vote down vote up
@Override
public void onOffsetChanged(AppBarLayout layout, int verticalOffset) {
    currentOffset = verticalOffset;

    final int insetTop = lastInsets != null ? lastInsets.getSystemWindowInsetTop() : 0;

    for (int i = 0, z = getChildCount(); i < z; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final ViewOffsetHelper offsetHelper = getViewOffsetHelper(child);

        switch (lp.collapseMode) {
            case LayoutParams.COLLAPSE_MODE_PIN:
                offsetHelper.setTopAndBottomOffset(
                    MathUtils.clamp(-verticalOffset, 0, getMaxOffsetForPinChild(child)));
                break;
            case LayoutParams.COLLAPSE_MODE_PARALLAX:
                offsetHelper.setTopAndBottomOffset(Math.round(-verticalOffset * lp.parallaxMult));
                break;
            default:
                break;
        }
    }

    // Show or hide the scrims if needed
    updateScrimVisibility();

    if (statusBarScrim != null && insetTop > 0) {
        ViewCompat.postInvalidateOnAnimation(SubtitleCollapsingToolbarLayout.this);
    }

    // Update the collapsing text's fraction
    final int expandRange = getHeight()
        - ViewCompat.getMinimumHeight(SubtitleCollapsingToolbarLayout.this)
        - insetTop;
    collapsingTextHelper.setExpansionFraction(Math.abs(verticalOffset) / (float) expandRange);
}
 
Example 7
Source File: CollapsingTitleBarLayout.java    From UIWidget with Apache License 2.0 5 votes vote down vote up
@Override
public void onOffsetChanged(AppBarLayout layout, int verticalOffset) {
    mCurrentOffset = verticalOffset;

    final int insetTop = mLastInsets != null ? mLastInsets.getSystemWindowInsetTop() : 0;

    for (int i = 0, z = getChildCount(); i < z; i++) {
        final View child = getChildAt(i);
        final LayoutParams lp = (LayoutParams) child.getLayoutParams();
        final ViewOffsetHelper offsetHelper = getViewOffsetHelper(child);

        switch (lp.mCollapseMode) {
            case CollapsingTitleBarLayout.LayoutParams.COLLAPSE_MODE_PIN:
                offsetHelper.setTopAndBottomOffset(constrain(-verticalOffset, 0, getMaxOffsetForPinChild(child)));
                break;
            case CollapsingTitleBarLayout.LayoutParams.COLLAPSE_MODE_PARALLAX:
                offsetHelper.setTopAndBottomOffset(
                        Math.round(-verticalOffset * lp.mParallaxMult));
                break;
        }
    }

    // Show or hide the scrims if needed
    updateScrimVisibility();

    if (mStatusBarScrim != null && insetTop > 0) {
        ViewCompat.postInvalidateOnAnimation(CollapsingTitleBarLayout.this);
    }

    // Update the collapsing text's fraction
    final int expandRange = getHeight() - ViewCompat.getMinimumHeight(
            CollapsingTitleBarLayout.this) - insetTop;
    mCollapsingTextHelper.setExpansionFraction(
            Math.abs(verticalOffset) / (float) expandRange);
}
 
Example 8
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the scroll range of all children.
 *
 * @return the scroll range in px
 */
public final int getTotalScrollRange() {
  if (totalScrollRange != INVALID_SCROLL_RANGE) {
    return totalScrollRange;
  }

  int range = 0;
  for (int i = 0, z = getChildCount(); i < z; i++) {
    final View child = getChildAt(i);
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final int childHeight = child.getMeasuredHeight();
    final int flags = lp.scrollFlags;

    if ((flags & LayoutParams.SCROLL_FLAG_SCROLL) != 0) {
      // We're set to scroll so add the child's height
      range += childHeight + lp.topMargin + lp.bottomMargin;

      if (i == 0 && ViewCompat.getFitsSystemWindows(child)) {
        // If this is the first child and it wants to handle system windows, we need to make
        // sure we don't scroll it past the inset
        range -= getTopInset();
      }
      if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) {
        // For a collapsing scroll, we to take the collapsed height into account.
        // We also break straight away since later views can't scroll beneath
        // us
        range -= ViewCompat.getMinimumHeight(child);
        break;
      }
    } else {
      // As soon as a view doesn't have the scroll flag, we end the range calculation.
      // This is because views below can not scroll under a fixed view.
      break;
    }
  }
  return totalScrollRange = Math.max(0, range);
}
 
Example 9
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/** Return the scroll range when scrolling down from a nested scroll. */
int getDownNestedScrollRange() {
  if (downScrollRange != INVALID_SCROLL_RANGE) {
    // If we already have a valid value, return it
    return downScrollRange;
  }

  int range = 0;
  for (int i = 0, z = getChildCount(); i < z; i++) {
    final View child = getChildAt(i);
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    int childHeight = child.getMeasuredHeight();
    childHeight += lp.topMargin + lp.bottomMargin;

    final int flags = lp.scrollFlags;

    if ((flags & LayoutParams.SCROLL_FLAG_SCROLL) != 0) {
      // We're set to scroll so add the child's height
      range += childHeight;

      if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) {
        // For a collapsing exit scroll, we to take the collapsed height into account.
        // We also break the range straight away since later views can't scroll
        // beneath us
        range -= ViewCompat.getMinimumHeight(child);
        break;
      }
    } else {
      // As soon as a view doesn't have the scroll flag, we end the range calculation.
      // This is because views below can not scroll under a fixed view.
      break;
    }
  }
  return downScrollRange = Math.max(0, range);
}
 
Example 10
Source File: CollapsingToolbarLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public void onOffsetChanged(AppBarLayout layout, int verticalOffset) {
  currentOffset = verticalOffset;

  final int insetTop = lastInsets != null ? lastInsets.getSystemWindowInsetTop() : 0;

  for (int i = 0, z = getChildCount(); i < z; i++) {
    final View child = getChildAt(i);
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final ViewOffsetHelper offsetHelper = getViewOffsetHelper(child);

    switch (lp.collapseMode) {
      case LayoutParams.COLLAPSE_MODE_PIN:
        offsetHelper.setTopAndBottomOffset(
            MathUtils.clamp(-verticalOffset, 0, getMaxOffsetForPinChild(child)));
        break;
      case LayoutParams.COLLAPSE_MODE_PARALLAX:
        offsetHelper.setTopAndBottomOffset(Math.round(-verticalOffset * lp.parallaxMult));
        break;
      default:
        break;
    }
  }

  // Show or hide the scrims if needed
  updateScrimVisibility();

  if (statusBarScrim != null && insetTop > 0) {
    ViewCompat.postInvalidateOnAnimation(CollapsingToolbarLayout.this);
  }

  // Update the collapsing text's fraction
  final int expandRange =
      getHeight() - ViewCompat.getMinimumHeight(CollapsingToolbarLayout.this) - insetTop;
  collapsingTextHelper.setExpansionFraction(Math.abs(verticalOffset) / (float) expandRange);
}
 
Example 11
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
/** Return the scroll range when scrolling down from a nested pre-scroll. */
int getDownNestedPreScrollRange() {
  if (downPreScrollRange != INVALID_SCROLL_RANGE) {
    // If we already have a valid value, return it
    return downPreScrollRange;
  }

  int range = 0;
  for (int i = getChildCount() - 1; i >= 0; i--) {
    final View child = getChildAt(i);
    final LayoutParams lp = (LayoutParams) child.getLayoutParams();
    final int childHeight = child.getMeasuredHeight();
    final int flags = lp.scrollFlags;

    if ((flags & LayoutParams.FLAG_QUICK_RETURN) == LayoutParams.FLAG_QUICK_RETURN) {
      // First take the margin into account
      int childRange = lp.topMargin + lp.bottomMargin;
      // The view has the quick return flag combination...
      if ((flags & LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED) != 0) {
        // If they're set to enter collapsed, use the minimum height
        childRange += ViewCompat.getMinimumHeight(child);
      } else if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) {
        // Only enter by the amount of the collapsed height
        childRange += childHeight - ViewCompat.getMinimumHeight(child);
      } else {
        // Else use the full height
        childRange += childHeight;
      }
      if (i == 0 && ViewCompat.getFitsSystemWindows(child)) {
        // If this is the first child and it wants to handle system windows, we need to make
        // sure we don't scroll past the inset
        childRange = Math.min(childRange, childHeight - getTopInset());
      }
      range += childRange;
    } else if (range > 0) {
      // If we've hit an non-quick return scrollable view, and we've already hit a
      // quick return view, return now
      break;
    }
  }
  return downPreScrollRange = Math.max(0, range);
}
 
Example 12
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private void snapToChildIfNeeded(CoordinatorLayout coordinatorLayout, @NonNull T abl) {
  final int offset = getTopBottomOffsetForScrollingSibling();
  final int offsetChildIndex = getChildIndexOnOffset(abl, offset);
  if (offsetChildIndex >= 0) {
    final View offsetChild = abl.getChildAt(offsetChildIndex);
    final LayoutParams lp = (LayoutParams) offsetChild.getLayoutParams();
    final int flags = lp.getScrollFlags();

    if ((flags & LayoutParams.FLAG_SNAP) == LayoutParams.FLAG_SNAP) {
      // We're set the snap, so animate the offset to the nearest edge
      int snapTop = -offsetChild.getTop();
      int snapBottom = -offsetChild.getBottom();

      if (offsetChildIndex == abl.getChildCount() - 1) {
        // If this is the last child, we need to take the top inset into account
        snapBottom += abl.getTopInset();
      }

      if (checkFlag(flags, LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED)) {
        // If the view is set only exit until it is collapsed, we'll abide by that
        snapBottom += ViewCompat.getMinimumHeight(offsetChild);
      } else if (checkFlag(
          flags, LayoutParams.FLAG_QUICK_RETURN | LayoutParams.SCROLL_FLAG_ENTER_ALWAYS)) {
        // If it's set to always enter collapsed, it actually has two states. We
        // select the state and then snap within the state
        final int seam = snapBottom + ViewCompat.getMinimumHeight(offsetChild);
        if (offset < seam) {
          snapTop = seam;
        } else {
          snapBottom = seam;
        }
      }

      if (checkFlag(flags, LayoutParams.SCROLL_FLAG_SNAP_MARGINS)) {
        // Update snap destinations to include margins
        snapTop += lp.topMargin;
        snapBottom -= lp.bottomMargin;
      }

      final int newOffset = offset < (snapBottom + snapTop) / 2 ? snapBottom : snapTop;
      animateOffsetTo(
          coordinatorLayout, abl, MathUtils.clamp(newOffset, -abl.getTotalScrollRange(), 0), 0);
    }
  }
}
 
Example 13
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private int interpolateOffset(@NonNull T layout, final int offset) {
  final int absOffset = Math.abs(offset);

  for (int i = 0, z = layout.getChildCount(); i < z; i++) {
    final View child = layout.getChildAt(i);
    final AppBarLayout.LayoutParams childLp = (LayoutParams) child.getLayoutParams();
    final Interpolator interpolator = childLp.getScrollInterpolator();

    if (absOffset >= child.getTop() && absOffset <= child.getBottom()) {
      if (interpolator != null) {
        int childScrollableHeight = 0;
        final int flags = childLp.getScrollFlags();
        if ((flags & LayoutParams.SCROLL_FLAG_SCROLL) != 0) {
          // We're set to scroll so add the child's height plus margin
          childScrollableHeight += child.getHeight() + childLp.topMargin + childLp.bottomMargin;

          if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) {
            // For a collapsing scroll, we to take the collapsed height
            // into account.
            childScrollableHeight -= ViewCompat.getMinimumHeight(child);
          }
        }

        if (ViewCompat.getFitsSystemWindows(child)) {
          childScrollableHeight -= layout.getTopInset();
        }

        if (childScrollableHeight > 0) {
          final int offsetForView = absOffset - child.getTop();
          final int interpolatedDiff =
              Math.round(
                  childScrollableHeight
                      * interpolator.getInterpolation(
                          offsetForView / (float) childScrollableHeight));

          return Integer.signum(offset) * (child.getTop() + interpolatedDiff);
        }
      }

      // If we get to here then the view on the offset isn't suitable for interpolated
      // scrolling. So break out of the loop
      break;
    }
  }

  return offset;
}
 
Example 14
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private void updateAppBarLayoutDrawableState(
    @NonNull final CoordinatorLayout parent,
    @NonNull final T layout,
    final int offset,
    final int direction,
    final boolean forceJump) {
  final View child = getAppBarChildOnOffset(layout, offset);
  if (child != null) {
    final AppBarLayout.LayoutParams childLp = (LayoutParams) child.getLayoutParams();
    final int flags = childLp.getScrollFlags();
    boolean lifted = false;

    if ((flags & LayoutParams.SCROLL_FLAG_SCROLL) != 0) {
      final int minHeight = ViewCompat.getMinimumHeight(child);

      if (direction > 0
          && (flags
                  & (LayoutParams.SCROLL_FLAG_ENTER_ALWAYS
                      | LayoutParams.SCROLL_FLAG_ENTER_ALWAYS_COLLAPSED))
              != 0) {
        // We're set to enter always collapsed so we are only collapsed when
        // being scrolled down, and in a collapsed offset
        lifted = -offset >= child.getBottom() - minHeight - layout.getTopInset();
      } else if ((flags & LayoutParams.SCROLL_FLAG_EXIT_UNTIL_COLLAPSED) != 0) {
        // We're set to exit until collapsed, so any offset which results in
        // the minimum height (or less) being shown is collapsed
        lifted = -offset >= child.getBottom() - minHeight - layout.getTopInset();
      }
    }

    if (layout.isLiftOnScroll()) {
      // Use first scrolling child as default scrolling view for updating lifted state because
      // it represents the content that would be scrolled beneath the app bar.
      lifted = layout.shouldLift(findFirstScrollingChild(parent));
    }

    final boolean changed = layout.setLiftedState(lifted);

    if (forceJump || (changed && shouldJumpElevationState(parent, layout))) {
      // If the collapsed state changed, we may need to
      // jump to the current state if we have an overlapping view
      layout.jumpDrawablesToCurrentState();
    }
  }
}