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

The following examples show how to use androidx.coordinatorlayout.widget.CoordinatorLayout#onLayoutChild() . 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: ExtendedFloatingActionButton.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onLayoutChild(
    @NonNull CoordinatorLayout parent,
    @NonNull ExtendedFloatingActionButton child,
    int layoutDirection) {
  // First, let's make sure that the visibility of the FAB is consistent
  final List<View> dependencies = parent.getDependencies(child);
  for (int i = 0, count = dependencies.size(); i < count; i++) {
    final View dependency = dependencies.get(i);
    if (dependency instanceof AppBarLayout) {
      if (updateFabVisibilityForAppBarLayout(parent, (AppBarLayout) dependency, child)) {
        break;
      }
    } else if (isBottomSheet(dependency)) {
      if (updateFabVisibilityForBottomSheet(dependency, child)) {
        break;
      }
    }
  }
  // Now let the CoordinatorLayout lay out the FAB
  parent.onLayoutChild(child, layoutDirection);
  return true;
}
 
Example 2
Source File: FloatingActionButton.java    From material-components-android with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onLayoutChild(
    @NonNull CoordinatorLayout parent,
    @NonNull FloatingActionButton child,
    int layoutDirection) {
  // First, let's make sure that the visibility of the FAB is consistent
  final List<View> dependencies = parent.getDependencies(child);
  for (int i = 0, count = dependencies.size(); i < count; i++) {
    final View dependency = dependencies.get(i);
    if (dependency instanceof AppBarLayout) {
      if (updateFabVisibilityForAppBarLayout(parent, (AppBarLayout) dependency, child)) {
        break;
      }
    } else if (isBottomSheet(dependency)) {
      if (updateFabVisibilityForBottomSheet(dependency, child)) {
        break;
      }
    }
  }
  // Now let the CoordinatorLayout lay out the FAB
  parent.onLayoutChild(child, layoutDirection);
  // Now offset it if needed
  offsetIfNeeded(parent, child);
  return true;
}
 
Example 3
Source File: BottomVerticalScrollBehavior.java    From BottomNavigation with Apache License 2.0 6 votes vote down vote up
@Override
public boolean onLayoutChild(CoordinatorLayout parent, final V child, int layoutDirection) {
    // First let the parent lay it out
    parent.onLayoutChild(child, layoutDirection);
    if (child instanceof BottomNavigationBar) {
        mViewRef = new WeakReference<>((BottomNavigationBar) child);
    }

    child.post(new Runnable() {
        @Override
        public void run() {
            mBottomNavHeight = child.getHeight();
        }
    });
    updateSnackBarPosition(parent, child, getSnackBarInstance(parent, child));

    return super.onLayoutChild(parent, child, layoutDirection);
}
 
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: BottomAppBar.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLayoutChild(
    @NonNull CoordinatorLayout parent, @NonNull BottomAppBar child, int layoutDirection) {
  viewRef = new WeakReference<>(child);

  View dependentView = child.findDependentView();
  if (dependentView != null && !ViewCompat.isLaidOut(dependentView)) {
    // Set the initial position of the FloatingActionButton with the BottomAppBar vertical
    // offset.
    CoordinatorLayout.LayoutParams fabLayoutParams =
        (CoordinatorLayout.LayoutParams) dependentView.getLayoutParams();
    fabLayoutParams.anchorGravity = Gravity.CENTER | Gravity.TOP;

    // Keep track of the original bottom margin for the fab. We will manage the margin if
    // nothing was set.
    originalBottomMargin = fabLayoutParams.bottomMargin;

    if (dependentView instanceof FloatingActionButton) {
      FloatingActionButton fab = ((FloatingActionButton) dependentView);

      // Always update the BAB if the fab is laid out.
      fab.addOnLayoutChangeListener(fabLayoutListener);

      // Ensure the FAB is correctly linked to this BAB so the animations can run correctly
      child.addFabAnimationListeners(fab);
    }

    // Move the fab to the correct position
    child.setCutoutState();
  }

  // Now let the CoordinatorLayout lay out the BAB
  parent.onLayoutChild(child, layoutDirection);
  return super.onLayoutChild(parent, child, layoutDirection);
}
 
Example 6
Source File: BottomNavBarFabBehaviour.java    From BottomNavigation with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLayoutChild(CoordinatorLayout parent, FloatingActionButton child, int layoutDirection) {
    // First let the parent lay it out
    parent.onLayoutChild(child, layoutDirection);

    updateFabTranslationForBottomNavigationBar(parent, child, null);

    return super.onLayoutChild(parent, child, layoutDirection);
}
 
Example 7
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 8
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 9
Source File: ViewOffsetBehavior.java    From material-components-android with Apache License 2.0 4 votes vote down vote up
protected void layoutChild(
    @NonNull CoordinatorLayout parent, @NonNull V child, int layoutDirection) {
  // Let the parent lay it out by default
  parent.onLayoutChild(child, layoutDirection);
}
 
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;
}