public override bool OnDependentViewChanged(CoordinatorLayout parent, Java.Lang.Object child, View dependency) { var view = child.JavaCast <TextView> (); int offset = dependency.Top - view.Top; ViewCompat.OffsetTopAndBottom(view, offset); return(true); }
public override bool OnLayoutChild(CoordinatorLayout parent, Java.Lang.Object cChild, int layoutDirection) { // return base.OnLayoutChild(parent, child, layoutDirection); var child = cChild.JavaCast <View>(); // First let the parent lay it out if (_state != StateDragging && _state != StateSettling) { if (parent.FitsSystemWindows && !child.FitsSystemWindows) { child.SetFitsSystemWindows(true); } parent.OnLayoutChild(child, layoutDirection); } // Offset the bottom sheet _parentHeight = parent.Height; _minOffset = System.Math.Max(0, _parentHeight - child.Height); _maxOffset = System.Math.Max(_parentHeight - _peekHeight, _minOffset); /** * New behavior */ switch (_state) { case StateAnchorPoint: ViewCompat.OffsetTopAndBottom(child, AnchorPoint); break; case StateExpanded: ViewCompat.OffsetTopAndBottom(child, _minOffset); break; case StateHidden: if (Hideable) { ViewCompat.OffsetTopAndBottom(child, _parentHeight); } break; case StateCollapsed: ViewCompat.OffsetTopAndBottom(child, _maxOffset); break; } ; if (_viewDragHelper == null) { _viewDragHelper = ViewDragHelper.Create(parent, _dragCallback); } _viewRef = new WeakReference <View>(child); _nestedScrollingChildRef = new WeakReference <View>(FindScrollingChild(child)); return(true); }
public override bool OnLayoutChild( CoordinatorLayout parent, Java.Lang.Object childObject, int layoutDirection) { Debug.WriteLineIf(DebugTrace, $"OnLayoutChild"); View child = Android.Runtime.Extensions.JavaCast <View>(childObject); if (ViewCompat.GetFitsSystemWindows(parent) && !ViewCompat.GetFitsSystemWindows(child)) { ViewCompat.SetFitsSystemWindows(child, true); } int savedTop = child.Top; // First let the parent lay it out parent.OnLayoutChild(child, layoutDirection); // Offset the bottom sheet mParentHeight = parent.Height; mMinOffset = Math.Max(0, mParentHeight - child.Height); mMaxOffset = Math.Max(mParentHeight - mPeekHeight, mMinOffset); mAnchorOffset = (int)Math.Max(mParentHeight * mAnchorThreshold, mMinOffset); Debug.WriteLineIf(DebugTrace, $"offset computed => savedTop:{savedTop} mMinOffset:{mMinOffset} mMaxOffset:{mMaxOffset} mAnchorOffset:{mAnchorOffset} "); if (mState == STATE_EXPANDED) { ViewCompat.OffsetTopAndBottom(child, mMinOffset); } else if (mState == STATE_ANCHOR) { ViewCompat.OffsetTopAndBottom(child, mAnchorOffset); } else if (mHideable && mState == STATE_HIDDEN) { ViewCompat.OffsetTopAndBottom(child, mParentHeight); } else if (mState == STATE_COLLAPSED) { ViewCompat.OffsetTopAndBottom(child, mMaxOffset); } else if (mState == STATE_DRAGGING || mState == STATE_SETTLING) { ViewCompat.OffsetTopAndBottom(child, savedTop - child.Top); } if (mViewDragHelper == null || mViewDragHelper.Handle == IntPtr.Zero) { mViewDragHelper = ViewDragHelper.Create(parent, mDragCallback); } mViewRef = new WeakReference <View>(child); mNestedScrollingChildRef = new WeakReference <View>(findScrollingChild(child)); return(true); }
public override bool OnLayoutChild(CoordinatorLayout parent, Java.Lang.Object child, int layoutDirection) { if (!(child is NestedScrollView view)) { return(base.OnLayoutChild(parent, child, layoutDirection)); } // First layout the child as normal. parent.OnLayoutChild(view, layoutDirection); // Center the FAB vertically along the top edge of the card. int fabHalfHeight = view.FindViewById(Resource.Id.fab).Height / 2; SetTopMargin(view.FindViewById(Resource.Id.cardview), fabHalfHeight); // Give the RecyclerView a maximum height to ensure the card will never // overlap the toolbar as it scrolls. int rvMaxHeight = view.Height - fabHalfHeight - view.FindViewById(Resource.Id.card_title).Height - view.FindViewById(Resource.Id.card_subtitle).Height; MaxHeightRecyclerView rv = view.FindViewById <MaxHeightRecyclerView>(Resource.Id.card_recyclerview); rv.SetMaxHeight(rvMaxHeight); // Give the card container top padding so that only the top edge of the card // initially appears at the bottom of the screen. The total padding will // be the distance from the top of the screen to the FAB's top edge. View cardContainer = view.FindViewById(Resource.Id.card_container); int toolbarContainerHeight = parent.GetDependencies(view)[0].Height; SetPaddingTop(cardContainer, rvMaxHeight - toolbarContainerHeight); // Offset the child's height so that its bounds don't overlap the // toolbar container. ViewCompat.OffsetTopAndBottom(view, toolbarContainerHeight); // Add the same amount of bottom padding to the RecyclerView so it doesn't // display its content underneath the navigation bar. SetPaddingBottom(rv, toolbarContainerHeight); // Return true so that the parent doesn't waste time laying out the // child again (any modifications made above will have triggered a second // layout pass anyway). return(true); }
protected override void OnCreate(Bundle savedInstanceState) { base.OnCreate(savedInstanceState); // Create your application here SetContentView(Resource.Layout.activity_coordinatorbehavior); ; var depentent = (TextView)FindViewById(Resource.Id.depentent); depentent.Click += (object sender, EventArgs e) => { ViewCompat.OffsetTopAndBottom(depentent, 5); }; //使用代码关联Behavior // var child = (TextView)FindViewById (Resource.Id.child); // CoordinatorLayout.LayoutParams _params = // (CoordinatorLayout.LayoutParams)child.LayoutParameters; // _params.Behavior = new CustomBehavior (); }
public override void OnNestedPreScroll( CoordinatorLayout coordinatorLayout, Java.Lang.Object childObject, View target, int dx, int dy, int[] consumed) { Debug.WriteLineIf(DebugTrace, $"OnNestedPreScroll"); View child = Android.Runtime.Extensions.JavaCast <View>(childObject); View scrollingChild; mNestedScrollingChildRef.TryGetTarget(out scrollingChild); if (target != scrollingChild) { return; } int currentTop = child.Top; int newTop = currentTop - dy; Debug.WriteLineIf(DebugTrace, $"currentTop:{currentTop} newTop:{newTop}"); if (dy > 0) { // Upward Debug.WriteLineIf(DebugTrace, $"dy > 0: Upward"); if (newTop < mMinOffset) { Debug.WriteLineIf(DebugTrace, $"newTop < mMinOffset: STATE_EXPANDED"); consumed[1] = currentTop - mMinOffset; ViewCompat.OffsetTopAndBottom(child, -consumed[1]); setStateInternal(STATE_EXPANDED); } else { Debug.WriteLineIf(DebugTrace, $"else: STATE_DRAGGING"); consumed[1] = dy; ViewCompat.OffsetTopAndBottom(child, -dy); setStateInternal(STATE_DRAGGING); } } else if (dy < 0) { // Downward Debug.WriteLineIf(DebugTrace, $"dy < 0: Downward"); if (!ViewCompat.CanScrollVertically(target, -1)) { if (newTop <= mMaxOffset || mHideable) { Debug.WriteLineIf(DebugTrace, $"newTop <= mMaxOffset || mHideable: STATE_DRAGGING"); consumed[1] = dy; ViewCompat.OffsetTopAndBottom(child, -dy); setStateInternal(STATE_DRAGGING); } else { Debug.WriteLineIf(DebugTrace, $"else: STATE_COLLAPSED"); consumed[1] = currentTop - mMaxOffset; ViewCompat.OffsetTopAndBottom(child, -consumed[1]); setStateInternal(STATE_COLLAPSED); } } } dispatchOnSlide(child.Top); mLastNestedScrollDy = dy; mNestedScrolled = true; }
public override void OnNestedPreScroll(CoordinatorLayout coordinatorLayout, Java.Lang.Object cChild, View target, int dx, int dy, int[] consumed, int type) { var child = cChild.JavaCast <View>(); _nestedScrollingChildRef.TryGetTarget(out View scrollingChild); if (target != scrollingChild) { return; } _scrollVelocityTracker.RecordScroll(dy); int currentTop = child.Top; int newTop = currentTop - dy; // Force stop at the anchor - do not go from collapsed to expanded in one scroll if ((_lastStableState == StateCollapsed && newTop < AnchorPoint) || (_lastStableState == StateExpanded && newTop > AnchorPoint)) { consumed[1] = dy; ViewCompat.OffsetTopAndBottom(child, AnchorPoint - currentTop); DispatchOnSlide(child.Top); _nestedScrolled = true; return; } if (dy > 0) { // Upward if (newTop < _minOffset) { consumed[1] = currentTop - _minOffset; ViewCompat.OffsetTopAndBottom(child, -consumed[1]); SetStateInternal(StateExpanded); } else { consumed[1] = dy; ViewCompat.OffsetTopAndBottom(child, -dy); SetStateInternal(StateDragging); } } else if (dy < 0) { // Downward if (!ViewCompat.CanScrollVertically(target, -1)) { if (newTop <= _maxOffset || Hideable) { // Restrict STATE_COLLAPSED if restrictedState is set if (Collapsible == true || (Collapsible == false && (AnchorPoint - newTop) >= 0)) { consumed[1] = dy; ViewCompat.OffsetTopAndBottom(child, -dy); SetStateInternal(StateDragging); } } else { consumed[1] = currentTop - _maxOffset; ViewCompat.OffsetTopAndBottom(child, -consumed[1]); SetStateInternal(StateCollapsed); } } } DispatchOnSlide(child.Top); _nestedScrolled = true; }