我正在使用RecyclerView来显示我的列表。我使用ItemTouchHelper在RecyclerView上实现滑动删除。通过使用canvas在OnchildDraw方法中实现底层布局。 现在我有一个问题:我想在我的图标上设置onclick事件,通过点击图标,我想执行一些功能。 这是我的类:
public class ItemTouchHelperCallback : ItemTouchHelper.SimpleCallback
{
private ContactSearchedResultAdapter _adapter;
private RecyclerView _mRecyclerView;
private int _swipeCount;
private Android.Content.Res.Resources _resources;
public ItemTouchHelperCallback(ContactSearchedResultAdapter adapter, RecyclerView mRecyclerView, Android.Content.Res.Resources resources)
: base(0, ItemTouchHelper.Left | ItemTouchHelper.Right)
{
this._adapter = adapter;
this._mRecyclerView = mRecyclerView;
this._resources = resources;
}
public override bool OnMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target)
{
return false;
}
public override void OnSwiped(RecyclerView.ViewHolder viewHolder, int direction)
{
if (direction == ItemTouchHelper.Left)
{
_adapter.RemoveViewWithDialog(viewHolder.AdapterPosition, _mRecyclerView, _swipeCount);
if (_swipeCount == 0)
_swipeCount++;
}
else
{
_adapter.SaveContactToDataBase(viewHolder.AdapterPosition, _mRecyclerView);
}
}
public override void OnChildDraw(Canvas cValue, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, bool isCurrentlyActive)
{
Paint paint = new Paint();
View itemView = viewHolder.ItemView;
float height = (float)itemView.Bottom - (float)itemView.Top;
float width = height / 3;
Bitmap icon;
if (dX > 0)
{
paint.Color = Color.ParseColor("#388E3C");
RectF background = new RectF((float)itemView.Left, (float)itemView.Top, dX, (float)itemView.Bottom);
cValue.DrawRect(background, paint);
icon = BitmapFactory.DecodeResource(_resources, Resource.Drawable.addoption);
RectF icon_dest = new RectF((float)itemView.Left + width, (float)itemView.Top + width, (float)itemView.Left + 2 * width, (float)itemView.Bottom - width);
cValue.DrawBitmap(icon, null, icon_dest, paint);
}
else
{
paint.Color = Color.ParseColor("#D32F2F");
RectF background = new RectF((float)itemView.Right + dX, (float)itemView.Top, (float)itemView.Right, (float)itemView.Bottom);
cValue.DrawRect(background, paint);
icon = BitmapFactory.DecodeResource(_resources, Resource.Drawable.removeoption);
RectF icon_dest = new RectF((float)itemView.Right - 2 * width, (float)itemView.Top + width, (float)itemView.Right - width, (float)itemView.Bottom - width);
cValue.DrawBitmap(icon, null, icon_dest, paint);
}
float alpha = (float)1.0- Math.Abs(dX)/(float) itemView.Width;
itemView.Alpha = alpha;
itemView.TranslationX = dX;
base.OnChildDraw(cValue, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive);
}
}
你可以看到,在OnSwiped方法中我调用了ItemRemoving或ItemSaving。现在我想要的是在通过canvas绘制的图标点击时调用这些方法。
我搜索了很多关于此主题的内容,但没有找到任何不使用库实现此功能的解决方案。
我不想使用库。