Swipeable List Item
A library that allows recycler views with swiping items to right or left for Android.
Supported Android versions
3.1.x and higher
Adding to your project
Add the dependency to your build.gradle:
dependencies {
compile 'com.samsaodev:swipeablelistitem:1.0.0'
}
Usage
- 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() {
}
});
If you don't add a swipe listener to a certain direction, it it the same as disabling it.
Customization
You can customize the views that show behind your cell when you swipe.
setRightSwipeBackground(ContextCompat.getDrawable(getContext(), R.color.red));
setRightSwipeText("delete");
setRightSwipeTextColor(ContextCompat.getColor(getContext(), android.R.color.white));
setRightSwipeTextSize((int) getResources().getDimension(R.dimen.font_size));
setRightSwipeTextTypeface(getTypeface());
setLeftDrawable(ContextCompat.getDrawable(getContext(), android.R.drawable.ic_delete));
setRightCustomView(LayoutInflater.from(getContext()).inflate(R.layout.custom_view, this, false));
License
SwipeableListItem is released under the MIT license. See the LICENSE file for details.