A library that allows recycler views with swiping items to right or left for Android.
3.1.x and higher
Add the dependency to your build.gradle:
dependencies {
compile 'com.samsaodev:swipeablelistitem:1.0.0'
}
- Create a view to be your RecyclerView's cell and extend it from SwipeableView. Then implement the method 'getContent(ViewGroup parent)' to provide your cell's layout. Your class should look like this:
public class ListItem extends SwipeableView {
public ListItem(Context context) {
super(context);
}
@Override
public View getContent(ViewGroup parent) {
return LayoutInflater.from(getContext()).inflate(R.layout.view_list_item, this, false);
}
}
- Add the following line to your RecylerView's setup:
recyclerView.addOnItemTouchListener(new SwipeableListOnItemTouchListener());
- To avoid problems with your list's cell recycling, call this every time you setup your cell in your ViewHolder class.
mListItem.resetSwipe();
- Set a listener to your cell to know when the user swiped to left, to right, or clicked on it. Example:
listItem.setRightSwipeListener(new RightSwipeListener() {
@Override
public void onRightSwipe() {
//Do something
}
});
If you don't add a swipe listener to a certain direction, it it the same as disabling it.
You can customize the views that show behind your cell when you swipe.
- Change the background
setRightSwipeBackground(ContextCompat.getDrawable(getContext(), R.color.red));
- Set a label:
setRightSwipeText("delete");
setRightSwipeTextColor(ContextCompat.getColor(getContext(), android.R.color.white));
setRightSwipeTextSize((int) getResources().getDimension(R.dimen.font_size));
setRightSwipeTextTypeface(getTypeface());
- Set an icon
setLeftDrawable(ContextCompat.getDrawable(getContext(), android.R.drawable.ic_delete));
- Set a custom view
setRightCustomView(LayoutInflater.from(getContext()).inflate(R.layout.custom_view, this, false));
SwipeableListItem is released under the MIT license. See the LICENSE file for details.