Working with recycler view is pretty easy and has made android programming a bit easy, but then also you need to know how exactly to use this powerful tool as per your wish.
To make items of a recycler view swipeable in any direction the google developers had provided you the a utility called the ItemTouchHelper which itself extends RecyclerView.ItemDecoration Class, so this can be applied to any recycler view and exactly in your activity where you set the adapter of your recycler view or any where in your activity and not in the adapter class of your recycler view.
With the introduction of this utility making the views of the recycler view swipeable has become very easy. In order to use this utility you first need to create a callback to this utility itself in which you need to override two basic methods of the callback class (namely onMove and onSwiped) plus another method (namely onChildDraw) if you wish to draw any child (or say paint the background to red) when any item is swiped.
Lets now have a look at the code :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
ItemTouchHelper.SimpleCallback simpleCallback = new ItemTouchHelper.SimpleCallback(0,ItemTouchHelper.LEFT) { @Override public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) { return false; } @Override public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) { final int position = viewHolder.getAdapterPosition(); if (direction == ItemTouchHelper.LEFT){ adapter.removeItem(position); } } @Override public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) { try { Bitmap icon; if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE){ View itemView = viewHolder.itemView; float height = (float) itemView.getBottom() - (float) itemView.getTop(); float width = height / 3; Paint paint = new Paint(); paint.setColor(Color.parseColor("#D32F2F")); RectF background = new RectF((float) itemView.getRight() + dX, (float) itemView.getTop(),(float) itemView.getRight(), (float) itemView.getBottom()); c.drawRect(background,paint); icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_delete); RectF destination = new RectF((float) itemView.getRight() - 2*width ,(float) itemView.getTop() + width,(float) itemView.getRight() - width,(float)itemView.getBottom() - width); c.drawBitmap(icon,null,destination ,paint); }else { super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isCurrentlyActive); } }catch (Exception e){ e.printStackTrace(); } } }; |
Explanation : this piece of code allows you to add a callback if your item is swiped in the left direction.
- onMove : this method allows you to move the whole item from one position to another in the adapter. I didn’t wanted this feature so i returned false here. Feel free to implement this method with your logic.
- onSwiped : this method is called when your item is swiped , i just wanted to remove the item when it is left swiped so i just checked the direction first then called the remove item of the adapter i created (this method just removes the item from the list associated with adapter and notifies the adapter that the item range has changed).
- onChildDraw : overriding this method is entirely optional and as per your UI design . I wanted to notify the user with a red background color so that it becomes quite understandable to the user that something is going to happen if he still swipes to the left.
With this being done you are all set to go, now you just have to attach this callback to a new ItemTouchHelper class object and associate this object with your recycler view and this is very easy .
Code to attach with recycler view :
1 2 |
ItemTouchHelper itemTouchHelper = new ItemTouchHelper(simpleCallback); itemTouchHelper.attachToRecyclerView(myRecyclerView); |
And all is done, build your project and see how it looks 🙂
Keep coding and keep sharing 🙂