Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swipe to another view in recyclerView with ItemTouchHelper

Given is a RecyclerView. What is the best practice and the easiest way to show a different View while the swipe is performed?

Here is a similar question. But with the solution there only a Bitmap can be shown.

With the recyclerview comes the awesome ItemTouchHelper, which has the callback:

 public void onChildDraw(Canvas canvas, RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, float dX, float dY, int actionState, boolean isCurrentlyActive) {...}

My question is: can i somehow exploit this callback to swipe between two views, and if yes, how.

Thank you.

like image 222
Paul Reznik Avatar asked Jul 02 '15 14:07

Paul Reznik


People also ask

What is recyclerview in SharePoint?

RecyclerView is an advanced and flexible version of ListView and GridView. It's capable of holding large amounts of list data and has better performance than its predecessors. As the name suggests, RecyclerView 'recycles' the items of our list once it's out of view on scrolling and re-populates them when they come back to view.

What is recyclerview and how to use it?

Here's what we want to achieve: RecyclerView is an advanced and flexible version of ListView and GridView. It's capable of holding large amounts of list data and has better performance than its predecessors.

How do I display trash can and archive icons on Swipe?

For left to right swipe, we’ll display a Trash can icon and from right to left swipe, we’ll display an Archive icon. If you watch step 2 of the code snippet, I’ve set the bounds for both icons.

How to get the menu layout from a list item swipe?

We call the showMenu () function inside our adapter when a list item is swiped. The onChildDraw () function draws the background while we swipe. Otherwise there'll be a white background while swiping and our menu layout will show up with a pop.


1 Answers

I am working on the similar task - need to reveal the hidden underlying panel while item is swiping away. I've managed to do it having two inner layouts inside the root RelativeLayout for item row view:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:fresco="http://schemas.android.com/apk/res-auto"
            xmlns:tools="http://schemas.android.com/tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

<RelativeLayout
    android:id="@+id/actionsLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hidden stuff is here"/>
</RelativeLayout>
<RelativeLayout android:id="@+id/itemLayout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:background="@color/submission_row_bg">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Swiping stuff is here"/>

</RelativeLayout>

</RelativeLayout>

In your ItemViewHolder class I have corresponding fields:

public class ItemViewHolder extends RecyclerView.ViewHolder {

    RelativeLayout itemLayout;       
    RelativeLayout actionsLayout;
}

Then inside ItemTouchHelper.Callback I am overriding onChildDraw method like this:

public static class MyItemTouchCallback extends ItemTouchHelper.Callback {

    public void onChildDraw(Canvas c, RecyclerView recyclerView,
            RecyclerView.ViewHolder viewHolder, float dX, float dY,
            int actionState, boolean isCurrentlyActive) {

        if (actionState == ItemTouchHelper.ACTION_STATE_SWIPE) {
            //HERE IS THE TRICK
            ((ItemViewHolder) viewHolder).itemLayout.setTranslationX(dX);

        } else {
            super.onChildDraw(c, recyclerView, viewHolder, dX, dY,
                    actionState, isCurrentlyActive);
        }
    }
}

It helps to swipe away one view and show the underlying one.

like image 102
elenatres Avatar answered Oct 12 '22 11:10

elenatres