5

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.

Community
  • 1
  • 1
Paul Reznik
  • 965
  • 1
  • 6
  • 18

1 Answers1

6

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.

elenatres
  • 304
  • 2
  • 6