Java Code Examples for androidx.coordinatorlayout.widget.CoordinatorLayout#getHeight()

The following examples show how to use androidx.coordinatorlayout.widget.CoordinatorLayout#getHeight() . 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: BottomSheetBehaviorGoogleMapsLike.java    From CustomBottomSheetBehavior with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLayoutChild( CoordinatorLayout parent, V child, int layoutDirection ) {
    // First let the parent lay it out
    if (mState != STATE_DRAGGING && mState != STATE_SETTLING) {
        if (parent.getFitsSystemWindows() &&
                !child.getFitsSystemWindows()) {
            child.setFitsSystemWindows(true);
        }
        parent.onLayoutChild(child, layoutDirection);
    }
    // Offset the bottom sheet
    mParentHeight = parent.getHeight();
    mMinOffset = Math.max(0, mParentHeight - child.getHeight());
    mMaxOffset = Math.max(mParentHeight - mPeekHeight, mMinOffset);

    /**
     * New behavior
     */
    if (mState == STATE_ANCHOR_POINT) {
        ViewCompat.offsetTopAndBottom(child, mAnchorPoint);
    } else if (mState == STATE_EXPANDED) {
        ViewCompat.offsetTopAndBottom(child, mMinOffset);
    } else if (mHideable && mState == STATE_HIDDEN) {
        ViewCompat.offsetTopAndBottom(child, mParentHeight);
    } else if (mState == STATE_COLLAPSED) {
        ViewCompat.offsetTopAndBottom(child, mMaxOffset);
    }
    if ( mViewDragHelper == null ) {
        mViewDragHelper = ViewDragHelper.create( parent, mDragCallback );
    }
    mViewRef = new WeakReference<>(child);
    mNestedScrollingChildRef = new WeakReference<>( findScrollingChild( child ) );
    return true;
}
 
Example 2
Source File: FabBehavior.java    From ZhiHuIndex with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onStartNestedScroll(CoordinatorLayout coordinatorLayout, View child, View directTargetChild, View target, int nestedScrollAxes) {

    if(child.getVisibility() == View.VISIBLE&& viewDistance ==0){
        //获取控件距离父布局(coordinatorLayout)底部距离
        viewDistance =coordinatorLayout.getHeight()-child.getY();
    }

    return (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;//判断是否竖直滚动
}
 
Example 3
Source File: FloatingActionButton.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
/**
 * Pre-Lollipop we use padding so that the shadow has enough space to be drawn. This method
 * offsets our layout position so that we're positioned correctly if we're on one of our
 * parent's edges.
 */
private void offsetIfNeeded(
    @NonNull CoordinatorLayout parent, @NonNull FloatingActionButton fab) {
  final Rect padding = fab.shadowPadding;

  if (padding != null && padding.centerX() > 0 && padding.centerY() > 0) {
    final CoordinatorLayout.LayoutParams lp =
        (CoordinatorLayout.LayoutParams) fab.getLayoutParams();

    int offsetTB = 0;
    int offsetLR = 0;

    if (fab.getRight() >= parent.getWidth() - lp.rightMargin) {
      // If we're on the right edge, shift it the right
      offsetLR = padding.right;
    } else if (fab.getLeft() <= lp.leftMargin) {
      // If we're on the left edge, shift it the left
      offsetLR = -padding.left;
    }
    if (fab.getBottom() >= parent.getHeight() - lp.bottomMargin) {
      // If we're on the bottom edge, shift it down
      offsetTB = padding.bottom;
    } else if (fab.getTop() <= lp.topMargin) {
      // If we're on the top edge, shift it up
      offsetTB = -padding.top;
    }

    if (offsetTB != 0) {
      ViewCompat.offsetTopAndBottom(fab, offsetTB);
    }
    if (offsetLR != 0) {
      ViewCompat.offsetLeftAndRight(fab, offsetLR);
    }
  }
}
 
Example 4
Source File: BottomSheetBehavior.java    From bottomsheetrecycler with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {
    if (ViewCompat.getFitsSystemWindows(parent) && !ViewCompat.getFitsSystemWindows(child)) {
        child.setFitsSystemWindows(true);
    }
    // Only set MaterialShapeDrawable as background if shapeTheming is enabled, otherwise will
    // default to android:background declared in styles or layout.
    if (shapeThemingEnabled && materialShapeDrawable != null) {
        ViewCompat.setBackground(child, materialShapeDrawable);
    }
    // Set elevation on MaterialShapeDrawable
    if (materialShapeDrawable != null) {
        // Use elevation attr if set on bottomsheet; otherwise, use elevation of child view.
        materialShapeDrawable.setElevation(
                elevation == -1 ? ViewCompat.getElevation(child) : elevation);
    }

    if (viewRef == null) {
        // First layout with this behavior.
        peekHeightMin =
                parent.getResources().getDimensionPixelSize(R.dimen.design_bottom_sheet_peek_height_min);
        viewRef = new WeakReference<>(child);
    }
    if (viewDragHelper == null) {
        viewDragHelper = ViewDragHelper.create(parent, dragCallback);
    }

    int savedTop = child.getTop();
    // First let the parent lay it out
    parent.onLayoutChild(child, layoutDirection);
    // Offset the bottom sheet
    parentWidth = parent.getWidth();
    parentHeight = parent.getHeight();
    fitToContentsOffset = Math.max(0, parentHeight - child.getHeight());
    calculateHalfExpandedOffset();
    calculateCollapsedOffset();

    if (state == STATE_EXPANDED) {
        ViewCompat.offsetTopAndBottom(child, getExpandedOffset());
    } else if (state == STATE_HALF_EXPANDED) {
        ViewCompat.offsetTopAndBottom(child, halfExpandedOffset);
    } else if (hideable && state == STATE_HIDDEN) {
        ViewCompat.offsetTopAndBottom(child, parentHeight);
    } else if (state == STATE_COLLAPSED) {
        ViewCompat.offsetTopAndBottom(child, collapsedOffset);
    } else if (state == STATE_DRAGGING || state == STATE_SETTLING) {
        ViewCompat.offsetTopAndBottom(child, savedTop - child.getTop());
    }

    //nestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
    return true;
}
 
Example 5
Source File: BottomSheetBehavior.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public boolean onLayoutChild(
    @NonNull CoordinatorLayout parent, @NonNull V child, int layoutDirection) {
  if (ViewCompat.getFitsSystemWindows(parent) && !ViewCompat.getFitsSystemWindows(child)) {
    child.setFitsSystemWindows(true);
  }

  if (viewRef == null) {
    // First layout with this behavior.
    peekHeightMin =
        parent.getResources().getDimensionPixelSize(R.dimen.design_bottom_sheet_peek_height_min);
    setSystemGestureInsets(parent);
    viewRef = new WeakReference<>(child);
    // Only set MaterialShapeDrawable as background if shapeTheming is enabled, otherwise will
    // default to android:background declared in styles or layout.
    if (shapeThemingEnabled && materialShapeDrawable != null) {
      ViewCompat.setBackground(child, materialShapeDrawable);
    }
    // Set elevation on MaterialShapeDrawable
    if (materialShapeDrawable != null) {
      // Use elevation attr if set on bottomsheet; otherwise, use elevation of child view.
      materialShapeDrawable.setElevation(
          elevation == -1 ? ViewCompat.getElevation(child) : elevation);
      // Update the material shape based on initial state.
      isShapeExpanded = state == STATE_EXPANDED;
      materialShapeDrawable.setInterpolation(isShapeExpanded ? 0f : 1f);
    }
    updateAccessibilityActions();
    if (ViewCompat.getImportantForAccessibility(child)
        == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
      ViewCompat.setImportantForAccessibility(child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
    }
  }
  if (viewDragHelper == null) {
    viewDragHelper = ViewDragHelper.create(parent, dragCallback);
  }

  int savedTop = child.getTop();
  // First let the parent lay it out
  parent.onLayoutChild(child, layoutDirection);
  // Offset the bottom sheet
  parentWidth = parent.getWidth();
  parentHeight = parent.getHeight();
  fitToContentsOffset = Math.max(0, parentHeight - child.getHeight());
  calculateHalfExpandedOffset();
  calculateCollapsedOffset();

  if (state == STATE_EXPANDED) {
    ViewCompat.offsetTopAndBottom(child, getExpandedOffset());
  } else if (state == STATE_HALF_EXPANDED) {
    ViewCompat.offsetTopAndBottom(child, halfExpandedOffset);
  } else if (hideable && state == STATE_HIDDEN) {
    ViewCompat.offsetTopAndBottom(child, parentHeight);
  } else if (state == STATE_COLLAPSED) {
    ViewCompat.offsetTopAndBottom(child, collapsedOffset);
  } else if (state == STATE_DRAGGING || state == STATE_SETTLING) {
    ViewCompat.offsetTopAndBottom(child, savedTop - child.getTop());
  }

  nestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
  return true;
}
 
Example 6
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
private boolean canScrollChildren(
    @NonNull CoordinatorLayout parent, @NonNull T child, @NonNull View directTargetChild) {
  return child.hasScrollableChildren()
      && parent.getHeight() - directTargetChild.getHeight() <= child.getHeight();
}
 
Example 7
Source File: BottomSheetBehavior.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public boolean onLayoutChild(
        @NonNull CoordinatorLayout parent, @NonNull V child, int layoutDirection) {
    if (ViewCompat.getFitsSystemWindows(parent) && !ViewCompat.getFitsSystemWindows(child)) {
        child.setFitsSystemWindows(true);
    }

    if (viewRef == null) {
        // First layout with this behavior.
        peekHeightMin =
                parent.getResources().getDimensionPixelSize(R.dimen.design_bottom_sheet_peek_height_min);
        viewRef = new WeakReference<>(child);
        // Only set MaterialShapeDrawable as background if shapeTheming is enabled, otherwise will
        // default to android:background declared in styles or layout.
        if (shapeThemingEnabled && materialShapeDrawable != null) {
            ViewCompat.setBackground(child, materialShapeDrawable);
        }
        // Set elevation on MaterialShapeDrawable
        if (materialShapeDrawable != null) {
            // Use elevation attr if set on bottomsheet; otherwise, use elevation of child view.
            materialShapeDrawable.setElevation(
                    elevation == -1 ? ViewCompat.getElevation(child) : elevation);
            // Update the material shape based on initial state.
            isShapeExpanded = state == STATE_EXPANDED;
            materialShapeDrawable.setInterpolation(isShapeExpanded ? 0f : 1f);
        }
        updateAccessibilityActions();
        if (ViewCompat.getImportantForAccessibility(child)
                == ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_AUTO) {
            ViewCompat.setImportantForAccessibility(child, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
        }
    }
    if (viewDragHelper == null) {
        viewDragHelper = ViewDragHelper.create(parent, dragCallback);
    }

    int savedTop = child.getTop();
    // First let the parent lay it out
    parent.onLayoutChild(child, layoutDirection);
    // Offset the bottom sheet
    parentWidth = parent.getWidth();
    parentHeight = parent.getHeight();
    fitToContentsOffset = Math.max(0, parentHeight - child.getHeight());
    calculateHalfExpandedOffset();
    calculateCollapsedOffset();

    if (state == STATE_EXPANDED) {
        ViewCompat.offsetTopAndBottom(child, getExpandedOffset());
    } else if (state == STATE_HALF_EXPANDED) {
        ViewCompat.offsetTopAndBottom(child, halfExpandedOffset);
    } else if (hideable && state == STATE_HIDDEN) {
        ViewCompat.offsetTopAndBottom(child, parentHeight);
    } else if (state == STATE_COLLAPSED) {
        ViewCompat.offsetTopAndBottom(child, collapsedOffset);
    } else if (state == STATE_DRAGGING || state == STATE_SETTLING) {
        ViewCompat.offsetTopAndBottom(child, savedTop - child.getTop());
    }

    nestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
    return true;
}