1. Are you ready for the Galaxy S20? Here is everything we know so far!

Left and Right Swipe in case of grid layout

Discussion in 'Android Development' started by minions95, Jun 15, 2017.

  1. minions95

    minions95 Lurker
    Thread Starter

    I am trying to implement left and right swipe action for recycler view with grid layout manager.

    My number of columns in the grid depends on the size of the screen. <600dp(1 column) and >600dp, <800dp(2 columns)

    In case of single column the left and right swipe animation is working properly. In case of 2 column grid, the swipe animations is not working fine.


    In the above image left swipe is performed on the "Hi" item.

    In the above image right swipe is performed on the "T3" item.

    I am using itemTouchHelper to perform the swipe action and onChildDraw is responsible for drawing the underlying layout and the code is as follows

    Code (Java):
    1. @Override
    2.             public void onChildDraw(Canvas c, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX,
    3.                                     float dY, int actionState, boolean isActive)
    4.             {
    5.                 if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
    6.                     View itemView = viewHolder.itemView;
    7.                     Paint p = new Paint();
    8.                     TextPaint textPaint = new TextPaint();
    9.                     Rect bounds = new Rect();
    10.                     if (dX < 0) {
    11.                         p.setColor(res.getColor(R.color.RED));                      
    12.                         c.drawRect((float) itemView.getRight() + dX, (float) itemView.getTop(),
    13.                                 (float) itemView.getRight(), (float) itemView.getBottom(), p);
    14.                         String showText = "Delete"
    15.                         textPaint.getTextBounds(showText, 0, showText.length(), bounds);
    16.                         c.drawText(showText, 0, showText.length(), itemView.getRight() + dX,
    17.                                 (itemView.getBottom()) - (itemView.getHeight() - bounds.height() >> 1), textPaint);
    18.                     }
    19.                     else {
    20.                         p.setColor(res.getColor(R.color.GREEN));
    21.                         c.drawRect((float) itemView.getLeft(), (float) itemView.getTop(), itemView.getLeft() + dX,
    22.                                 (float) itemView.getBottom(), p);
    23.                         String showText = "Mark"
    24.                         int margin = (int) (itemView.getHeight() * 0.2);
    25.                         textPaint.setTextSize(margin);
    26.                         textPaint.getTextBounds(showText, 0, showText.length(), bounds);
    27.                         c.drawText(showText, 0, showText.length(), itemView.getLeft(),
    28.                                 (itemView.getBottom()) - (itemView.getHeight() - bounds.height() >> 1), textPaint);
    29.                     }
    30.                 }
    31.                 super.onChildDraw(c, recyclerView, viewHolder, dX, dY, actionState, isActive);
    32.             }
    When right or left swipe is performed, I want the swiped view to go below the neighboring view instead of overlapping on top of it. Can someone let me know how to achieve this?

    1. Download the Forums for Android™ app!



Share This Page