Fah*_*imi 6 android xamarin.android android-recyclerview
我正在使用 RecyclerView 来显示我的列表。我使用 ItemTouchHelper 实现 Swipe 以关闭 RecyclerView。底层布局是通过使用画布在 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);
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,我正在 OnSwiped 方法中调用 ItemRemoving 或 ItemSaving。我现在想要做的是在点击图标时调用这些方法(在 OnchildDraw 中由画布绘制的图标)我搜索了很多关于这个主题的内容,但找不到任何不使用任何库就实现了这个功能的解决方案。我不想使用图书馆。
I had the same problem as yours. I coded half a day and finally find one way.
First of all, I think this is android ItemTouchHelper problem that there are no methods to solve that.
So it can't use ItemTouchHelper and other system methods.
What I do is this: 1. Set recylerView.setOnTouchListener to get (x,y) on screen.
When ItemTouchHelper onSwiped(), set it a state and find the clickable region.
检查您的触摸点是否碰到图标区域。
在 onTouch() 方法中被击中时做一些事情。
您可以在我的 Github 存储库中找到我的解决方案。 https://github.com/TindleWei/WindGod
这里有 3 种使用 ItemTouchHelper 的方法:
像你这样的安卓正常方式
前景和背景区域方式
浏览器方式
在我的回购中,您可以查看第二种方式。
| 归档时间: |
|
| 查看次数: |
10018 次 |
| 最近记录: |