Android RecyclerView with Swipe layout

Droid By Me
2 min readOct 9, 2019

We have showed many more RecyclerView examples earlier like Android RecyclerView, Android CardView with RecyclerView, Android RecyclerView with multiple view type and Android RecyclerView with Single and Multiple Selection.

Now we have another article behind this RecyclerView is with swipeable item. Many more times, you have requirements to do swipe for item action like edit, delete or anything else. For that, we have swipeable layout for each recyclerview item. So let’s begin to swipe.

First, add dependency library for swipeable layout

implementation 'com.chauthai.swipereveallayout:swipe-reveal-layout:1.4.1'

Then create your xml for recyclerview item like below:

<?xml version="1.0" encoding="utf-8"?>
<com.chauthai.swipereveallayout.SwipeRevealLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/swipelayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:dragEdge="right"
app:mode="same_level"
>

<!--Swipe Layout-->
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">

</LinearLayout>

<!--Main Layout-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal"
android:padding="20dp">

</LinearLayout>

</com.chauthai.swipereveallayout.SwipeRevealLayout>

So this is the important to know that we have 2 layouts. One is swipe layout and visible on swipe action on the item. Second one is main layout that actually visible for recyclerview item. Make sure to put a correct height and width attribute values for swipe layout (wrap_content, match_parent). Its very important. Otherwise swipe layout doesn’t visible with proper behavior.

Also check the bold highlighted code in xml. dragEdge has 4 values right , left, top and bottom for swiping item from different edges. mode has 2 values, same_level and normal.

SwipeAdapter.java

package com.droidbyme.recyclerviewselection.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.chauthai.swipereveallayout.SwipeRevealLayout;
import com.chauthai.swipereveallayout.ViewBinderHelper;
import com.droidbyme.recyclerviewselection.R;
import com.droidbyme.recyclerviewselection.model.Employee;

import java.util.ArrayList;

public class SwipeAdapter extends RecyclerView.Adapter<SwipeAdapter.SwipeViewHolder> {

private Context context;
private ArrayList<Employee> employees;
private final ViewBinderHelper viewBinderHelper = new ViewBinderHelper();

public SwipeAdapter(Context context, ArrayList<Employee> employees) {
this.context = context;
this.employees = employees;
}

public void setEmployees(ArrayList<Employee> employees) {
this.employees = new ArrayList<>();
this.employees = employees;
notifyDataSetChanged();
}

@NonNull
@Override
public SwipeViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(context).inflate(R.layout.item_employee_swipe, viewGroup, false);
return new SwipeViewHolder(view);
}

@Override
public void onBindViewHolder(@NonNull SwipeViewHolder swipeViewHolder, int i) {
viewBinderHelper.setOpenOnlyOne(true);
viewBinderHelper.bind(swipeViewHolder.swipelayout, String.valueOf(employees.get(i).getName()));
viewBinderHelper.closeLayout(String.valueOf(employees.get(i).getName()));

swipeViewHolder.bindData(employees.get(i));
}

@Override
public int getItemCount() {
return employees.size();
}

class SwipeViewHolder extends RecyclerView.ViewHolder {

private TextView textView;
private SwipeRevealLayout swipelayout;

SwipeViewHolder(@NonNull View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
swipelayout = itemView.findViewById(R.id.swipelayout);
}

void bindData(Employee employee) {
textView.setText(employee.getName());
}
}
}

viewBinderHelper.setOpenOnlyOne(true); is for opening only one swipe layout at a time.

viewBinderHelper.bind(); is for save/restore open/close state of the view. It means if you open 1st item then scroll and again come back to the 1st item, its already opened. If you do not write this line, open/close behavior is not same as you did previously. And you need to pass any unique id as string for helper bind. There is no change in activity code. Check the output:

Find source here.

--

--