Swipe different view in recyclerView with ItemTouchHelper

Provided by RecyclerView. What's the best practice and easiest way to show a different View while swiping is running?

There is a similar question here. But with a solution, only a bitmap can be shown there.

With recyclerview comes awesome ItemTouchHelper which has a 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 use this callback to scroll between two views, and if so, how.

Thank.

+3


source to share


1 answer


I am working on a similar task - need to reveal the hidden base pane while the element is bouncing. I was able to do this with two internal layouts inside the root of the RelativeLayout to view the position row:

<?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 the ItemViewHolder class, I have the corresponding fields:

public class ItemViewHolder extends RecyclerView.ViewHolder {

    RelativeLayout itemLayout;       
    RelativeLayout actionsLayout;
}

      



Then, inside ItemTouchHelper.Callback, I override the 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);
        }
    }
}

      

This helps to draw one view and show the main one.

+4


source







All Articles