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

The following examples show how to use androidx.core.view.ViewCompat#offsetTopAndBottom() . 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: CustomSwipeRefreshLayout.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
void setTargetOffsetTopAndBottom(int offset, boolean requiresUpdate) {
    mCircleView.bringToFront();
    ViewCompat.offsetTopAndBottom(mCircleView, offset);
    mCurrentTargetOffsetTop = mCircleView.getTop();
    if (requiresUpdate && android.os.Build.VERSION.SDK_INT < 11) {
        invalidate();
    }
}
 
Example 2
Source File: VRefreshLayout.java    From BaseProject with Apache License 2.0 5 votes vote down vote up
private void moveTo(int y) {
    int dy = y - mHeaderCurrentTop;
    ViewCompat.offsetTopAndBottom(mHeaderView, dy);
    //直接调用autoRefresh()时,这里mContentView 会空指针
    ViewCompat.offsetTopAndBottom(mContentView, dy);
    mHeaderCurrentTop = mHeaderView.getTop();
    mProgress.currentY = mHeaderCurrentTop - mHeaderOrginTop;
    notifyProgress();
}
 
Example 3
Source File: ViewOverlayApi14.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
public void add(View child) {
  assertNotDisposed();
  if (child.getParent() instanceof ViewGroup) {
    ViewGroup parent = (ViewGroup) child.getParent();
    if (parent != hostView
        && parent.getParent() != null
        && ViewCompat.isAttachedToWindow(parent)) {
      // Moving to different container; figure out how to position child such that
      // it is in the same location on the screen
      int[] parentLocation = new int[2];
      int[] hostViewLocation = new int[2];
      parent.getLocationOnScreen(parentLocation);
      hostView.getLocationOnScreen(hostViewLocation);
      ViewCompat.offsetLeftAndRight(child, parentLocation[0] - hostViewLocation[0]);
      ViewCompat.offsetTopAndBottom(child, parentLocation[1] - hostViewLocation[1]);
    }
    parent.removeView(child);
    //                if (parent.getLayoutTransition() != null) {
    //                    // LayoutTransition will cause the child to delay removal - cancel it
    //                    parent.getLayoutTransition().cancel(LayoutTransition.DISAPPEARING);
    //                }
    // fail-safe if view is still attached for any reason
    if (child.getParent() != null) {
      parent.removeView(child);
    }
  }
  super.addView(child);
}
 
Example 4
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 5
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void offsetChildAsNeeded(@NonNull View child, @NonNull View dependency) {
  final CoordinatorLayout.Behavior behavior =
      ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior();
  if (behavior instanceof BaseBehavior) {
    // Offset the child, pinning it to the bottom the header-dependency, maintaining
    // any vertical gap and overlap
    final BaseBehavior ablBehavior = (BaseBehavior) behavior;
    ViewCompat.offsetTopAndBottom(
        child,
        (dependency.getBottom() - child.getTop())
            + ablBehavior.offsetDelta
            + getVerticalLayoutGap()
            - getOverlapPixelsForOffset(dependency));
  }
}
 
Example 6
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 7
Source File: AppBarLayout.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
  super.onLayout(changed, l, t, r, b);

  if (ViewCompat.getFitsSystemWindows(this) && shouldOffsetFirstChild()) {
    // If we need to offset the first child, we need to offset all of them to make space
    final int topInset = getTopInset();
    for (int z = getChildCount() - 1; z >= 0; z--) {
      ViewCompat.offsetTopAndBottom(getChildAt(z), topInset);
    }
  }

  invalidateScrollRanges();

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

    if (interpolator != null) {
      haveChildWithInterpolator = true;
      break;
    }
  }

  if (statusBarForeground != null) {
    statusBarForeground.setBounds(0, 0, getWidth(), getTopInset());
  }

  // If the user has set liftable manually, don't set liftable state automatically.
  if (!liftableOverride) {
    setLiftableState(liftOnScroll || hasCollapsibleChild());
  }
}
 
Example 8
Source File: BottomSheetBehavior.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
public void onNestedPreScroll(
    @NonNull CoordinatorLayout coordinatorLayout,
    @NonNull V child,
    @NonNull View target,
    int dx,
    int dy,
    @NonNull int[] consumed,
    int type) {
  if (type == ViewCompat.TYPE_NON_TOUCH) {
    // Ignore fling here. The ViewDragHelper handles it.
    return;
  }
  View scrollingChild = nestedScrollingChildRef != null ? nestedScrollingChildRef.get() : null;
  if (target != scrollingChild) {
    return;
  }
  int currentTop = child.getTop();
  int newTop = currentTop - dy;
  if (dy > 0) { // Upward
    if (newTop < getExpandedOffset()) {
      consumed[1] = currentTop - getExpandedOffset();
      ViewCompat.offsetTopAndBottom(child, -consumed[1]);
      setStateInternal(STATE_EXPANDED);
    } else {
      if (!draggable) {
        // Prevent dragging
        return;
      }

      consumed[1] = dy;
      ViewCompat.offsetTopAndBottom(child, -dy);
      setStateInternal(STATE_DRAGGING);
    }
  } else if (dy < 0) { // Downward
    if (!target.canScrollVertically(-1)) {
      if (newTop <= collapsedOffset || hideable) {
        if (!draggable) {
          // Prevent dragging
          return;
        }

        consumed[1] = dy;
        ViewCompat.offsetTopAndBottom(child, -dy);
        setStateInternal(STATE_DRAGGING);
      } else {
        consumed[1] = currentTop - collapsedOffset;
        ViewCompat.offsetTopAndBottom(child, -consumed[1]);
        setStateInternal(STATE_COLLAPSED);
      }
    }
  }
  dispatchOnSlide(child.getTop());
  lastNestedScrollDy = dy;
  nestedScrolled = true;
}
 
Example 9
Source File: BottomSheetBehavior.java    From Mysplash with GNU Lesser General Public License v3.0 4 votes vote down vote up
@Override
public void onNestedPreScroll(
        @NonNull CoordinatorLayout coordinatorLayout,
        @NonNull V child,
        @NonNull View target,
        int dx,
        int dy,
        @NonNull int[] consumed,
        int type) {
    if (type == ViewCompat.TYPE_NON_TOUCH) {
        // Ignore fling here. The ViewDragHelper handles it.
        return;
    }
    View scrollingChild = nestedScrollingChildRef != null ? nestedScrollingChildRef.get() : null;
    if (target != scrollingChild) {
        return;
    }
    int currentTop = child.getTop();
    int newTop = currentTop - dy;
    if (dy > 0) { // Upward
        if (newTop < getExpandedOffset()) {
            consumed[1] = currentTop - getExpandedOffset();
            ViewCompat.offsetTopAndBottom(child, -consumed[1]);
            setStateInternal(STATE_EXPANDED);
        } else {
            if (!draggable) {
                // Prevent dragging
                return;
            }

            consumed[1] = dy;
            ViewCompat.offsetTopAndBottom(child, -dy);
            setStateInternal(STATE_DRAGGING);
        }
    } else if (dy < 0) { // Downward
        if (!target.canScrollVertically(-1)) {
            if (newTop <= collapsedOffset || hideable) {
                if (!draggable) {
                    // Prevent dragging
                    return;
                }

                consumed[1] = dy;
                ViewCompat.offsetTopAndBottom(child, -dy);
                setStateInternal(STATE_DRAGGING);
            } else {
                consumed[1] = currentTop - collapsedOffset;
                ViewCompat.offsetTopAndBottom(child, -consumed[1]);
                setStateInternal(STATE_COLLAPSED);
            }
        }
    }
    dispatchOnSlide(child.getTop());
    lastNestedScrollDy = dy;
    nestedScrolled = true;
}
 
Example 10
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;
}
 
Example 11
Source File: CollapsingToolbarLayout.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
  super.onLayout(changed, left, top, right, bottom);

  if (lastInsets != null) {
    // Shift down any views which are not set to fit system windows
    final int insetTop = lastInsets.getSystemWindowInsetTop();
    for (int i = 0, z = getChildCount(); i < z; i++) {
      final View child = getChildAt(i);
      if (!ViewCompat.getFitsSystemWindows(child)) {
        if (child.getTop() < insetTop) {
          // If the child isn't set to fit system windows but is drawing within
          // the inset offset it down
          ViewCompat.offsetTopAndBottom(child, insetTop);
        }
      }
    }
  }

  // Update our child view offset helpers so that they track the correct layout coordinates
  for (int i = 0, z = getChildCount(); i < z; i++) {
    getViewOffsetHelper(getChildAt(i)).onViewLayout();
  }

  // Update the collapsed bounds by getting its transformed bounds
  if (collapsingTitleEnabled && dummyView != null) {
    // We only draw the title if the dummy view is being displayed (Toolbar removes
    // views if there is no space)
    drawCollapsingTitle =
        ViewCompat.isAttachedToWindow(dummyView) && dummyView.getVisibility() == VISIBLE;

    if (drawCollapsingTitle) {
      final boolean isRtl =
          ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;

      // Update the collapsed bounds
      final int maxOffset =
          getMaxOffsetForPinChild(toolbarDirectChild != null ? toolbarDirectChild : toolbar);
      DescendantOffsetUtils.getDescendantRect(this, dummyView, tmpRect);
      collapsingTextHelper.setCollapsedBounds(
          tmpRect.left + (isRtl ? toolbar.getTitleMarginEnd() : toolbar.getTitleMarginStart()),
          tmpRect.top + maxOffset + toolbar.getTitleMarginTop(),
          tmpRect.right + (isRtl ? toolbar.getTitleMarginStart() : toolbar.getTitleMarginEnd()),
          tmpRect.bottom + maxOffset - toolbar.getTitleMarginBottom());

      // Update the expanded bounds
      collapsingTextHelper.setExpandedBounds(
          isRtl ? expandedMarginEnd : expandedMarginStart,
          tmpRect.top + expandedMarginTop,
          right - left - (isRtl ? expandedMarginStart : expandedMarginEnd),
          bottom - top - expandedMarginBottom);
      // Now recalculate using the new bounds
      collapsingTextHelper.recalculate();
    }
  }

  // Set our minimum height to enable proper AppBarLayout collapsing
  if (toolbar != null) {
    if (collapsingTitleEnabled && TextUtils.isEmpty(collapsingTextHelper.getText())) {
      // If we do not currently have a title, try and grab it from the Toolbar
      setTitle(toolbar.getTitle());
    }
    if (toolbarDirectChild == null || toolbarDirectChild == this) {
      setMinimumHeight(getHeightWithMargins(toolbar));
    } else {
      setMinimumHeight(getHeightWithMargins(toolbarDirectChild));
    }
  }

  updateScrimVisibility();

  // Apply any view offsets, this should be done at the very end of layout
  for (int i = 0, z = getChildCount(); i < z; i++) {
    getViewOffsetHelper(getChildAt(i)).applyOffsets();
  }
}
 
Example 12
Source File: ViewOffsetHelper.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
void applyOffsets() {
  ViewCompat.offsetTopAndBottom(view, offsetTop - (view.getTop() - layoutTop));
  ViewCompat.offsetLeftAndRight(view, offsetLeft - (view.getLeft() - layoutLeft));
}
 
Example 13
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 14
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 15
Source File: BottomSheetBehaviorGoogleMapsLike.java    From CustomBottomSheetBehavior with Apache License 2.0 4 votes vote down vote up
@Override
public void onNestedPreScroll( CoordinatorLayout coordinatorLayout, V child, View target,
                               int dx, int dy, int[] consumed,
                               @ViewCompat.NestedScrollType int type) {
    View scrollingChild = mNestedScrollingChildRef.get();
    if ( target != scrollingChild ) {
        return;
    }

    mScrollVelocityTracker.recordScroll( dy );

    int currentTop = child.getTop();
    int newTop     = currentTop - dy;

    // Force stop at the anchor - do not go from collapsed to expanded in one scroll
    if (
            ( mLastStableState == STATE_COLLAPSED  &&  newTop < mAnchorPoint )  ||
                    ( mLastStableState == STATE_EXPANDED   &&  newTop > mAnchorPoint )
            ) {
        consumed[1] = dy;
        ViewCompat.offsetTopAndBottom( child, mAnchorPoint - currentTop );
        dispatchOnSlide( child.getTop() );
        mNestedScrolled = true;
        return;
    }

    if ( dy > 0 ) { // Upward
        if ( newTop < mMinOffset ) {
            consumed[1] = currentTop - mMinOffset;
            ViewCompat.offsetTopAndBottom( child, -consumed[1] );
            setStateInternal( STATE_EXPANDED );
        } else {
            consumed[1] = dy;
            ViewCompat.offsetTopAndBottom( child, -dy );
            setStateInternal( STATE_DRAGGING );
        }
    } else if (dy < 0) { // Downward
        if (!ViewCompat.canScrollVertically(target, -1)) {
            if (newTop <= mMaxOffset || mHideable) {
                // Restrict STATE_COLLAPSED if restrictedState is set
                if(mCollapsible==true || (mCollapsible==false && (mAnchorPoint - newTop)>=0)) {
                    consumed[1] = dy;
                    ViewCompat.offsetTopAndBottom(child, -dy);
                    setStateInternal(STATE_DRAGGING);
                }
            } else {
                consumed[1] = currentTop - mMaxOffset;
                ViewCompat.offsetTopAndBottom(child, -consumed[1]);
                setStateInternal(STATE_COLLAPSED);
            }
        }
    }
    dispatchOnSlide(child.getTop());
    mNestedScrolled = true;
}
 
Example 16
Source File: ViewOffsetHelper.java    From UIWidget with Apache License 2.0 4 votes vote down vote up
private void updateOffsets() {
    ViewCompat.offsetTopAndBottom(mView, mOffsetTop - (mView.getTop() - mLayoutTop));
    ViewCompat.offsetLeftAndRight(mView, mOffsetLeft - (mView.getLeft() - mLayoutLeft));
}
 
Example 17
Source File: CollapsingTitleBarLayout.java    From UIWidget with Apache License 2.0 4 votes vote down vote up
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);
    int topBarInsetAdjustTop = 0;
    if (mLastInsets != null) {
        // Shift down any views which are not set to fit system windows
        final int insetTop = mLastInsets.getSystemWindowInsetTop();
        for (int i = 0, z = getChildCount(); i < z; i++) {
            final View child = getChildAt(i);
            if (ViewCompat.getFitsSystemWindows(child)) {
                if (child.getTop() < insetTop) {
                    // If the child isn't set to fit system windows but is drawing within
                    // the inset offset it down
                    ViewCompat.offsetTopAndBottom(child, insetTop);
                }
            }
        }
        View adjustView = mTitleBarDirectChild != null ? mTitleBarDirectChild : mTitleBar;
        if (ViewCompat.getFitsSystemWindows(adjustView)) {
            topBarInsetAdjustTop = insetTop;
        }
    }

    // Update the collapsed bounds by getting it's transformed bounds
    if (mCollapsingTitleEnabled) {
        // Update the collapsed bounds
        final int maxOffset = getMaxOffsetForPinChild(
                mTitleBarDirectChild != null ? mTitleBarDirectChild : mTitleBar);
        ViewGroupUtils.getDescendantRect(this, mTitleBar, mTmpRect);
        mTmpRect.top = mTmpRect.top - topBarInsetAdjustTop;
        Rect rect = mTitleBar.getTitleContainerRect();
        int horStart = mTmpRect.top + maxOffset;
        mCollapsingTextHelper.setCollapsedBounds(
                mTmpRect.left + rect.left,
                horStart + rect.top,
                mTmpRect.left + rect.right,
                horStart + rect.bottom);

        // Update the expanded bounds
        mCollapsingTextHelper.setExpandedBounds(
                mExpandedMarginStart,
                mTmpRect.top + mExpandedMarginTop,
                right - left - mExpandedMarginEnd,
                bottom - top - mExpandedMarginBottom);
        // Now recalculate using the new bounds
        mCollapsingTextHelper.recalculate();
    }

    // Update our child view offset helpers. This needs to be done after the title has been
    // setup, so that any Toolbars are in their original position
    for (int i = 0, z = getChildCount(); i < z; i++) {
        getViewOffsetHelper(getChildAt(i)).onViewLayout();
    }

    // Finally, set our minimum height to enable proper AppBarLayout collapsing
    if (mTitleBar != null) {
        if (mCollapsingTitleEnabled && TextUtils.isEmpty(mCollapsingTextHelper.getText())) {
            // If we do not currently have a title, try and grab it from the Toolbar
            mCollapsingTextHelper.setText(mTitleBar.getTextView(Gravity.CENTER | Gravity.TOP).getText());
        }
        if (mTitleBarDirectChild == null || mTitleBarDirectChild == this) {
            setMinimumHeight(getHeightWithMargins(mTitleBar));
        } else {
            setMinimumHeight(getHeightWithMargins(mTitleBarDirectChild));
        }
        mTitleBar.setCenterGravityLeft(getCollapsedTitleGravity() == Gravity.LEFT);
    }

    updateScrimVisibility();
}
 
Example 18
Source File: SubtitleCollapsingToolbarLayout.java    From collapsingtoolbarlayout-subtitle with Apache License 2.0 4 votes vote down vote up
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    super.onLayout(changed, left, top, right, bottom);

    if (lastInsets != null) {
        // Shift down any views which are not set to fit system windows
        final int insetTop = lastInsets.getSystemWindowInsetTop();
        for (int i = 0, z = getChildCount(); i < z; i++) {
            final View child = getChildAt(i);
            if (!ViewCompat.getFitsSystemWindows(child)) {
                if (child.getTop() < insetTop) {
                    // If the child isn't set to fit system windows but is drawing within
                    // the inset offset it down
                    ViewCompat.offsetTopAndBottom(child, insetTop);
                }
            }
        }
    }

    // Update our child view offset helpers so that they track the correct layout coordinates
    for (int i = 0, z = getChildCount(); i < z; i++) {
        getViewOffsetHelper(getChildAt(i)).onViewLayout();
    }

    // Update the collapsed bounds by getting its transformed bounds
    if (collapsingTitleEnabled && dummyView != null) {
        // We only draw the title if the dummy view is being displayed (Toolbar removes
        // views if there is no space)
        drawCollapsingTitle = ViewCompat.isAttachedToWindow(dummyView) && dummyView.getVisibility() == VISIBLE;

        if (drawCollapsingTitle) {
            final boolean isRtl = ViewCompat.getLayoutDirection(this) == ViewCompat.LAYOUT_DIRECTION_RTL;

            // Update the collapsed bounds
            final int maxOffset =
                getMaxOffsetForPinChild(toolbarDirectChild != null ? toolbarDirectChild : toolbar);
            DescendantOffsetUtils.getDescendantRect(this, dummyView, tmpRect);
            collapsingTextHelper.setCollapsedBounds(
                tmpRect.left + (isRtl ? toolbar.getTitleMarginEnd() : toolbar.getTitleMarginStart()),
                tmpRect.top + maxOffset + toolbar.getTitleMarginTop(),
                tmpRect.right + (isRtl ? toolbar.getTitleMarginStart() : toolbar.getTitleMarginEnd()),
                tmpRect.bottom + maxOffset - toolbar.getTitleMarginBottom());

            // Update the expanded bounds
            collapsingTextHelper.setExpandedBounds(
                isRtl ? expandedMarginEnd : expandedMarginStart,
                tmpRect.top + expandedMarginTop,
                right - left - (isRtl ? expandedMarginStart : expandedMarginEnd),
                bottom - top - expandedMarginBottom);
            // Now recalculate using the new bounds
            collapsingTextHelper.recalculate();
        }
    }

    // Set our minimum height to enable proper AppBarLayout collapsing
    if (toolbar != null) {
        if (collapsingTitleEnabled && TextUtils.isEmpty(collapsingTextHelper.getTitle())) {
            // If we do not currently have a title, try and grab it from the Toolbar
            setTitle(toolbar.getTitle());
            setSubtitle(toolbar.getSubtitle());
        }
        if (toolbarDirectChild == null || toolbarDirectChild == this) {
            setMinimumHeight(getHeightWithMargins(toolbar));
        } else {
            setMinimumHeight(getHeightWithMargins(toolbarDirectChild));
        }
    }

    updateScrimVisibility();

    // Apply any view offsets, this should be done at the very end of layout
    for (int i = 0, z = getChildCount(); i < z; i++) {
        getViewOffsetHelper(getChildAt(i)).applyOffsets();
    }
}
 
Example 19
Source File: BottomSheetBehavior.java    From bottomsheetrecycler with Apache License 2.0 4 votes vote down vote up
@Override
public void onNestedPreScroll(
        @NonNull CoordinatorLayout coordinatorLayout,
        @NonNull V child,
        @NonNull View target,
        int dx,
        int dy,
        @NonNull int[] consumed,
        int type) {
    if (type == ViewCompat.TYPE_NON_TOUCH) {
        // Ignore fling here. The ViewDragHelper handles it.
        return;
    }
    //View scrollingChild = nestedScrollingChildRef != null ? nestedScrollingChildRef.get() : null;
    if (!isOneOfChild(target)) {
        return;
    }
    int currentTop = child.getTop();
    int newTop = currentTop - dy;
    if (dy > 0) { // Upward
        if (newTop < getExpandedOffset()) {
            consumed[1] = currentTop - getExpandedOffset();
            ViewCompat.offsetTopAndBottom(child, -consumed[1]);
            setStateInternal(STATE_EXPANDED);
        } else {
            consumed[1] = dy;
            ViewCompat.offsetTopAndBottom(child, -dy);
            setStateInternal(STATE_DRAGGING);
        }
    } else if (dy < 0) { // Downward
        if (!target.canScrollVertically(-1)) {
            if (newTop <= collapsedOffset || hideable) {
                consumed[1] = dy;
                ViewCompat.offsetTopAndBottom(child, -dy);
                setStateInternal(STATE_DRAGGING);
            } else {
                consumed[1] = currentTop - collapsedOffset;
                ViewCompat.offsetTopAndBottom(child, -consumed[1]);
                setStateInternal(STATE_COLLAPSED);
            }
        }
    }
    dispatchOnSlide(child.getTop());
    lastNestedScrollDy = dy;
    nestedScrolled = true;
}