Creating buttons and clicking a button under the recyclerview element while scrolling it (switching to dismissal) without using a library

I use RecyclerView to display my list. I am implementing Swipe for rejection in RecyclerView using ItemTouchHelper. The main layout is implemented in the OnchildDraw method using a canvas. Now I have a problem: I want to install onclick on my icon. By clicking on the icon, I want to perform some functions. Here is my class:

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); } } 

As you can see, I call ItemRemoving or ItemSaving in the OnSwiped method. What I want to do now is call these methods when I click on the icons (the icons that are drawn by the canvas in OnchildDraw) I searched a lot for this topic and did not find a solution that implemented this function without using any library. I do not want to use the library.

+2
android xamarin.android android-recyclerview
source share
2 answers

I had the same problem as you. I encoded half a day and finally found one way.

First of all, I think this is the android ItemTouchHelper problem, that there are no methods to solve this problem.

Therefore, it cannot use ItemTouchHelper and other system methods.

What I am doing is: 1. Set recylerView.setOnTouchListener to get (x, y) on the screen.

  1. When ItemTouchHelper onSwiped (), set it to state and find the area that you can click into.

  2. Check your touch point if it falls into the icon area.

  3. Do something when you use the onTouch () method.

You can find my solution in my Github registry. https://github.com/TindleWei/WindGod

Here are 3 ways to use ItemTouchHelper:

  • The usual Android way, like yours

  • foreground and background path

  • viewpager way

In my repo you can see the second way.

+1
source share

For those using AdamWei Solution # 2, if you are having problems with the recyclerview ontouchlistener in the snippet, change the following line:

 Point point = new Point((int)motionEvent.getRawX(), (int)motionEvent.getRawY()); 

to

 Point point = new Point((int) motionEvent.getX(), (int) motionEvent.getY()); 
+1
source share

All Articles