public override void OnChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive) { if (actionState == ItemTouchHelper.ActionStateSwipe) { viewHolder.ItemView.TranslationX = dX; if (isCurrentlyActive) { MainActivity.instance.contentRefresh.Enabled = false; } else { MainActivity.instance.contentRefresh.Enabled = true; } ColorDrawable background = new ColorDrawable(Color.Red); if (dX < 0) { background.SetBounds(viewHolder.ItemView.Right + (int)dX, viewHolder.ItemView.Top, viewHolder.ItemView.Right, viewHolder.ItemView.Bottom); drawable.SetBounds(viewHolder.ItemView.Right - MainActivity.instance.DpToPx(52), viewHolder.ItemView.Top + (viewHolder.ItemView.Bottom - viewHolder.ItemView.Top - MainActivity.instance.DpToPx(36)) / 2, viewHolder.ItemView.Right - MainActivity.instance.DpToPx(16), viewHolder.ItemView.Top + (viewHolder.ItemView.Bottom - viewHolder.ItemView.Top + MainActivity.instance.DpToPx(36)) / 2); } else { background.SetBounds(viewHolder.ItemView.Left, viewHolder.ItemView.Top, viewHolder.ItemView.Left + (int)dX, viewHolder.ItemView.Bottom); drawable.SetBounds(viewHolder.ItemView.Left + MainActivity.instance.DpToPx(16), viewHolder.ItemView.Top + (viewHolder.ItemView.Bottom - viewHolder.ItemView.Top - MainActivity.instance.DpToPx(36)) / 2, viewHolder.ItemView.Left + MainActivity.instance.DpToPx(52), viewHolder.ItemView.Top + (viewHolder.ItemView.Bottom - viewHolder.ItemView.Top + MainActivity.instance.DpToPx(36)) / 2); } background.Draw(c); drawable.Draw(c); } else { base.OnChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } }
public override void OnChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive) { base.OnChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); var itemView = viewHolder.ItemView; var offset = 20; if (dX < 0) { _background.SetBounds(itemView.Right + ((int)dX - offset), itemView.Top, itemView.Right, itemView.Bottom); var margin = (itemView.Height - _icon.IntrinsicHeight) / 2; var top = itemView.Top + (itemView.Height - _icon.IntrinsicHeight) / 2; var bottom = top + _icon.IntrinsicHeight; var left = itemView.Right - margin - _icon.IntrinsicWidth; var right = itemView.Right - margin; _icon.SetBounds(left, top, right, bottom); } else { _background.SetBounds(0, 0, 0, 0); } _background.Draw(c); _icon.Draw(c); }
public override void OnChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive) { try { if (actionState == ItemTouchHelper.ActionStateSwipe) { View itemView = viewHolder.ItemView; Drawable background = new ColorDrawable(); ((ColorDrawable)background).Color = GetBgColorCode(); if (dX > 0) { // swipe right background.SetBounds(itemView.Left, itemView.Top, (int)dX, itemView.Bottom); } else { // swipe left background.SetBounds(itemView.Right + (int)dX, itemView.Top, itemView.Right, itemView.Bottom); } background.Draw(c); } base.OnChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } catch (Exception e) { Console.WriteLine(e); } }
public override void OnChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive) { base.OnChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); View itemView = viewHolder.ItemView; int itemHeight = itemView.Height; Boolean isCancelled = dX == 0 && !isCurrentlyActive; if (isCancelled) { clearCanvas(c, itemView.Right + dX, (float)itemView.Top, (float)itemView.Right, (float)itemView.Bottom); base.OnChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); return; } mBackground.SetColorFilter(Color.Red, PorterDuff.Mode.Add); mBackground.SetBounds(itemView.Right + (int)dX, itemView.Top, itemView.Right, itemView.Bottom); mBackground.Draw(c); int deleteIconTop = itemView.Top + (itemHeight - intrinsicHeight) / 2; int deleteIconMargin = (itemHeight - intrinsicHeight) / 2; int deleteIconLeft = itemView.Right - deleteIconMargin - intrinsicWidth; int deleteIconRight = itemView.Right - deleteIconMargin; int deleteIconBottom = deleteIconTop + intrinsicHeight; deleteDrawable.SetBounds(deleteIconLeft, deleteIconTop, deleteIconRight, deleteIconBottom); deleteDrawable.Draw(c); base.OnChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); }
public override void OnChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive) { base.OnChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); var itemView = viewHolder.ItemView; var itemHeight = itemView.Bottom - itemView.Top; // Draw the green edit background var background = new ColorDrawable { Color = Color.ParseColor("#4CAF50") }; background.SetBounds(itemView.Left, itemView.Top, itemView.Left + (int)dX, itemView.Bottom); background.Draw(c); var context = Mvx.IoCProvider.Resolve <IMvxAndroidCurrentTopActivity>().Activity; var editIcon = ContextCompat.GetDrawable(context, Resource.Drawable.baseline_edit_white_24); // Calculate position of edit icon var iconTop = itemView.Top + (itemHeight - editIcon.IntrinsicHeight) / 2; var iconMargin = (itemHeight + editIcon.IntrinsicHeight) / 2; var iconLeft = itemView.Left + iconMargin - editIcon.IntrinsicWidth; var iconRight = itemView.Left + iconMargin; var iconBottom = iconTop + editIcon.IntrinsicHeight; // Draw the edit icon editIcon.SetBounds(iconLeft, iconTop, iconRight, iconBottom); editIcon.Draw(c); }
private void DrawBackground(Canvas c, RectF rect) { using (var colorDrawable = new ColorDrawable(BackgroundColor)) { colorDrawable.SetBounds((int)rect.Left, (int)rect.Top, (int)rect.Right, (int)rect.Bottom); colorDrawable.Draw(c); } }
/** * Decorate the RecyclerView item with the chosen backgrounds and icons */ public void Decorate() { try { if (_actionState != ItemTouchHelper.ActionStateSwipe) { return; } if (_dX > 0) { // Swiping Right if (_swipeRightBackgroundColor != null) { var background = new ColorDrawable(_swipeRightBackgroundColor.Value); background.SetBounds(_viewHolder.ItemView.Left, _viewHolder.ItemView.Top, _viewHolder.ItemView.Left + (int)_dX, _viewHolder.ItemView.Bottom); background.Draw(_canvas); } var iconSize = 0; if (_swipeRightActionIconId != 0 && _dX > _iconHorizontalMargin) { var icon = ContextCompat.GetDrawable(_recyclerView.Context, _swipeRightActionIconId); if (icon != null) { iconSize = icon.IntrinsicHeight; var halfIcon = iconSize / 2; var top = _viewHolder.ItemView.Top + ((_viewHolder.ItemView.Bottom - _viewHolder.ItemView.Top) / 2 - halfIcon); icon.SetBounds(_viewHolder.ItemView.Left + _iconHorizontalMargin, top, _viewHolder.ItemView.Left + _iconHorizontalMargin + icon.IntrinsicWidth, top + icon.IntrinsicHeight); if (_swipeRightActionIconTint != null) { icon.SetColorFilter(_swipeRightActionIconTint.Value, PorterDuff.Mode.SrcIn); } icon.Draw(_canvas); } } if (!string.IsNullOrEmpty(_mSwipeRightText) && _dX > _iconHorizontalMargin + iconSize) { var textPaint = new TextPaint { AntiAlias = true, TextSize = TypedValue.ApplyDimension(_mSwipeRightTextUnit, _mSwipeRightTextSize, _recyclerView.Context.Resources.DisplayMetrics), Color = _mSwipeRightTextColor }; textPaint.SetTypeface(_mSwipeRightTypeface); var textTop = (int)(_viewHolder.ItemView.Top + (_viewHolder.ItemView.Bottom - _viewHolder.ItemView.Top) / 2.0 + textPaint.TextSize / 2); _canvas.DrawText(_mSwipeRightText, _viewHolder.ItemView.Left + _iconHorizontalMargin + iconSize + (iconSize > 0 ? _iconHorizontalMargin / 2 : 0), textTop, textPaint); } } else if (_dX < 0) { // Swiping Left if (_swipeLeftBackgroundColor != null) { var background = new ColorDrawable(_swipeLeftBackgroundColor.Value); background.SetBounds(_viewHolder.ItemView.Right + (int)_dX, _viewHolder.ItemView.Top, _viewHolder.ItemView.Right, _viewHolder.ItemView.Bottom); background.Draw(_canvas); } var iconSize = 0; var imgLeft = _viewHolder.ItemView.Right; if (_swipeLeftActionIconId != 0 && _dX < -_iconHorizontalMargin) { var icon = ContextCompat.GetDrawable(_recyclerView.Context, _swipeLeftActionIconId); if (icon != null) { iconSize = icon.IntrinsicHeight; var halfIcon = iconSize / 2; var top = _viewHolder.ItemView.Top + ((_viewHolder.ItemView.Bottom - _viewHolder.ItemView.Top) / 2 - halfIcon); imgLeft = _viewHolder.ItemView.Right - _iconHorizontalMargin - halfIcon * 2; icon.SetBounds(imgLeft, top, _viewHolder.ItemView.Right - _iconHorizontalMargin, top + icon.IntrinsicHeight); if (_swipeLeftActionIconTint != null) { icon.SetColorFilter(_swipeLeftActionIconTint.Value, PorterDuff.Mode.SrcIn); } icon.Draw(_canvas); } } if (!string.IsNullOrEmpty(_mSwipeLeftText) && _dX < -_iconHorizontalMargin - iconSize) { var textPaint = new TextPaint { AntiAlias = true, TextSize = TypedValue.ApplyDimension(_mSwipeLeftTextUnit, _mSwipeLeftTextSize, _recyclerView.Context.Resources.DisplayMetrics), Color = _mSwipeLeftTextColor }; textPaint.SetTypeface(_mSwipeLeftTypeface); var width = textPaint.MeasureText(_mSwipeLeftText); var textTop = (int)(_viewHolder.ItemView.Top + (_viewHolder.ItemView.Bottom - _viewHolder.ItemView.Top) / 2.0 + textPaint.TextSize / 2); _canvas.DrawText(_mSwipeLeftText, imgLeft - width - (imgLeft == _viewHolder.ItemView.Right ? _iconHorizontalMargin : _iconHorizontalMargin / 2), textTop, textPaint); } } } catch (Exception e) { } }
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { var orientation = 0; int headerCount = 0, footerCount = 0, dataCount; var adapterObj = parent.GetAdapter(); var adapterType = adapterObj.GetType(); if (adapterType.IsAssignableFrom(typeof(RecyclerArrayAdapter <>))) { headerCount = (int)adapterType.GetMethod("GetHeaderCount").Invoke(adapterObj, null); footerCount = (int)adapterType.GetMethod("GetFooterCount").Invoke(adapterObj, null); dataCount = (int)adapterType.GetMethod("GetCount").Invoke(adapterObj, null); } else { dataCount = parent.GetAdapter().ItemCount; } var dataStartPosition = headerCount; var dataEndPosition = headerCount + dataCount; var layoutManager = parent.GetLayoutManager(); switch (layoutManager) { case StaggeredGridLayoutManager manager: orientation = manager.Orientation; break; case GridLayoutManager gridLayoutManager: orientation = gridLayoutManager.Orientation; break; case LinearLayoutManager linearLayoutManager: orientation = linearLayoutManager.Orientation; break; } int start, end; if (orientation == OrientationHelper.Vertical) { start = parent.PaddingLeft + _paddingLeft; end = parent.Width - parent.PaddingRight - _paddingRight; } else { start = parent.PaddingTop + _paddingLeft; end = parent.Height - parent.PaddingBottom - _paddingRight; } var childCount = parent.ChildCount; for (var i = 0; i < childCount; i++) { var child = parent.GetChildAt(i); var position = parent.GetChildAdapterPosition(child); if ((position < dataStartPosition || position >= dataEndPosition - 1) && (position != dataEndPosition - 1 || !_drawLastItem) && (position >= dataStartPosition && position < dataEndPosition || !_drawHeaderFooter)) { continue; } if (orientation == OrientationHelper.Vertical) { var layoutParams = (RecyclerView.LayoutParams)child.LayoutParameters; var top = child.Bottom + layoutParams.BottomMargin; var bottom = top + _height; _colorDrawable.SetBounds(start, top, end, bottom); _colorDrawable.Draw(c); } else { var layoutParams = (RecyclerView.LayoutParams)child.LayoutParameters; var left = child.Right + layoutParams.RightMargin; var right = left + _height; _colorDrawable.SetBounds(left, start, right, end); _colorDrawable.Draw(c); } } }
public override void OnChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive) { if (actionState == ItemTouchHelper.ActionStateSwipe) { // Fade out the view as it is swiped out of the parent's bounds //float alpha = AlphaFull - Math.Abs(dX) / (float)viewHolder.ItemView.Width; //viewHolder.ItemView.Alpha = alpha; //viewHolder.ItemView.TranslationX = dX; //SetTouchListener(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } else { } //if (CurrentViewHolder != null && CurrentViewHolder.GetHashCode() != viewHolder.GetHashCode()) //{ // ClearView(recyclerView, CurrentViewHolder); // CurrentViewHolder = null; //} CurrentViewHolder = viewHolder; View itemView = viewHolder.ItemView; int backgroundCornerOffset = 20; //so background is behind the rounded corners of itemView //int iconHeight = _leftToRightIcon.IntrinsicHeight; //int iconWidth = _leftToRightIcon.IntrinsicWidth; int iconHeight = Dp2px(36); int iconWidth = Dp2px(36); //235px int iconMargin = (itemView.Height - iconHeight) / 2; int iconTop = itemView.Top + (itemView.Height - iconHeight) / 2; int iconBottom = iconTop + iconHeight; float newDx = dX; bool swypingToTheRight = dX > 0; int maxWidth = (int)(Math.Abs(viewHolder.ItemView.Width * 0.7) * (swypingToTheRight ? 1 : -1)); //System.Diagnostics.Debug.WriteLine("Dx = {0} y maxWidth = {1}", dX, maxWidth); //System.Diagnostics.Debug.WriteLine("ItemLeft = {0}, ItemTop = {1}, ItemRight = {2}, ItemBottom = {3}", itemView.Left, itemView.Top, itemView.Right, itemView.Bottom); //DrawButtons(c, viewHolder); if (swypingToTheRight) { _swypeIsHappening = true; int iconLeft = itemView.Left + iconMargin; int iconRight = iconLeft + iconWidth; //_leftButton.Top = itemView.Top; //_leftButton.Bottom = itemView.Bottom; //_leftButton.Left = iconLeft; //_leftButton.Right = iconRight; //System.Diagnostics.Debug.WriteLine("Swyping to the right. Icon Left = {0}, icon right = {1}, iconTop = {2}, iconBottom = {3}", iconLeft, iconRight, iconTop, iconBottom); _leftToRightBackground.SetBounds( itemView.Left, itemView.Top, itemView.Left + ((int)dX) + backgroundCornerOffset, itemView.Bottom); _leftToRightBackground.Draw(c); //_leftToRightIcon.Draw(c); for (int i = 0; i < 1; i++) { iconLeft = (int)(itemView.Left + (i + 1) * iconMargin); iconRight = iconLeft + iconWidth; _leftToRightIcon.SetBounds(iconLeft, iconTop, iconRight, iconBottom); _leftToRightIcon.Draw(c); } //_leftToRightIcon.Draw(c); //_leftButton.Draw(c); } else if (dX < 0) // Swiping to the left { _swypeIsHappening = true; int iconLeft = itemView.Right - iconMargin - iconWidth; int iconRight = itemView.Right - iconMargin; //System.Diagnostics.Debug.WriteLine("Swyping to the left. Icon Left = {0}, icon right = {1}, iconTop = {2}, iconBottom = {3}", iconLeft, iconRight, iconTop, iconBottom); _rightToLefIcon.SetBounds(iconLeft, iconTop, iconRight, iconBottom); _rightToLeftbackground.SetBounds( itemView.Right + ((int)dX) - backgroundCornerOffset, itemView.Top, itemView.Right, itemView.Bottom); _rightToLeftbackground.Draw(c); _rightToLefIcon.Draw(c); } else { // view is unSwiped _leftToRightBackground.SetBounds(0, 0, 0, 0); _rightToLeftbackground.SetBounds(0, 0, 0, 0); _swypeIsHappening = false; } //if (newDx != 0 && (swypingToTheRight && newDx >= maxWidth || !swypingToTheRight && newDx < maxWidth)) //{ // _reachedMaxSwypeWidth = true; // newDx = maxWidth; // //itemView.TranslationX = maxWidth; // return; //} //else //{ // _reachedMaxSwypeWidth = false; //} base.OnChildDraw(c, recyclerView, viewHolder, newDx, dY, actionState, isCurrentlyActive); //System.Diagnostics.Debug.WriteLine("-----------OnChildDraw was called"); }
public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) { int orientation = 0; int headerCount = 0, footerCount = 0, dataCount; var adapterObj = parent.GetAdapter(); var adapterType = adapterObj.GetType(); if (adapterType.IsAssignableFrom(typeof(RecyclerArrayAdapter <>))) { headerCount = (int)adapterType.GetMethod("GetHeaderCount").Invoke(adapterObj, null); footerCount = (int)adapterType.GetMethod("GetFooterCount").Invoke(adapterObj, null); dataCount = (int)adapterType.GetMethod("GetCount").Invoke(adapterObj, null); } else { dataCount = parent.GetAdapter().ItemCount; } int dataStartPosition = headerCount; int dataEndPosition = headerCount + dataCount; RecyclerView.LayoutManager layoutManager = parent.GetLayoutManager(); if (layoutManager is StaggeredGridLayoutManager) { orientation = ((StaggeredGridLayoutManager)layoutManager).Orientation; } else if (layoutManager is GridLayoutManager) { orientation = ((GridLayoutManager)layoutManager).Orientation; } else if (layoutManager is LinearLayoutManager) { orientation = ((LinearLayoutManager)layoutManager).Orientation; } int start, end; if (orientation == OrientationHelper.Vertical) { start = parent.PaddingLeft + mPaddingLeft; end = parent.Width - parent.PaddingRight - mPaddingRight; } else { start = parent.PaddingTop + mPaddingLeft; end = parent.Height - parent.PaddingBottom - mPaddingRight; } int childCount = parent.ChildCount; for (int i = 0; i < childCount; i++) { View child = parent.GetChildAt(i); int position = parent.GetChildAdapterPosition(child); if (position >= dataStartPosition && position < dataEndPosition - 1 ||//数据项除了最后一项 (position == dataEndPosition - 1 && mDrawLastItem) || //数据项最后一项 (!(position >= dataStartPosition && position < dataEndPosition) && mDrawHeaderFooter) //header&footer且可绘制 ) { if (orientation == OrientationHelper.Vertical) { RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)child.LayoutParameters; int top = child.Bottom + layoutParams.BottomMargin; int bottom = top + mHeight; mColorDrawable.SetBounds(start, top, end, bottom); mColorDrawable.Draw(c); } else { RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams)child.LayoutParameters; int left = child.Right + layoutParams.RightMargin; int right = left + mHeight; mColorDrawable.SetBounds(left, start, right, end); mColorDrawable.Draw(c); } } } }