SimpleRecyclerView
A RecyclerView extension for building list more easily.
Install / Use
/learn @jaychang0917/SimpleRecyclerViewREADME
SimpleRecyclerView
A RecyclerView extension for building list more easily.
Screenshot
Table of Contents
- Basic Usage
- Multiple Types
- Cell Operations
- Divider
- Spacing
- Empty State View
- Section Header
- Auto Load More
- Drag & Drop
- Swipe To Dismiss
- Snappy
- References
Sample Project
<a href='https://play.google.com/store/apps/details?id=com.jaychang.demo.srv&hl=en'> <img alt='Get it on Google Play' src='https://play.google.com/intl/en_us/badges/images/generic/en_badge_web_generic.png' height="116" width="300"/> </a>Installation
In your app level build.gradle :
dependencies {
implementation 'com.jaychang:simplerecyclerview:2.0.5'
// for kotlin android extensions
implementation 'com.jaychang:simplerecyclerview-kotlin-android-extensions:2.0.5'
}
<a name=basic_usage>Basic Usage</a>
Basically, there are three steps to build your list.
1. Configure the SimpleRecyclerView
<com.jaychang.srv.SimpleRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srv_layoutMode="linearVertical|linearHorizontal|grid"
app:srv_gridSpanCount="integer"
app:srv_gridSpanSequence="integer string (e.g. 2233)"
... />
2. Define the cell by extending SimpleCell<T, VH>
/**
* Accept two type arguments,
* 1st is the data model this cell represents, 2nd is the view holder.
* */
public class BookCell extends SimpleCell<Book, BookCell.ViewHolder> {
/**
* Mandatory constructor, pass your data model as argument
* */
public BookCell(Book item) {
super(item);
}
/**
* Define the layout resource of this cell
* */
@Override
protected int getLayoutRes() {
return R.layout.cell_book;
}
@NonNull
@Override
protected ViewHolder onCreateViewHolder(ViewGroup parent, View cellView) {
return new ViewHolder(cellView);
}
@Override
protected void onBindViewHolder(ViewHolder holder, int position, Context context, Object payload) {
holder.textView.setText(getItem().getTitle());
}
/**
* Optional
* */
@Override
protected void onUnbindViewHolder(ViewHolder holder) {
// do your cleaning jobs here when the item view is recycled.
}
/**
* Define your view holder, which must extend SimpleViewHolder.
* */
static class ViewHolder extends SimpleViewHolder {
TextView textView;
ViewHolder(View itemView) {
super(itemView);
textView = itemView.findViewById(R.id.textView);
}
}
}
<a name=kotlin_support>Kotlin android extensions support</a>
If you are using Kotlin android extensions, the cell can be simplified as following:
class BookCell(item: Book) : SimpleCell<Book>(item) {
override fun getLayoutRes(): Int {
return R.layout.cell_book
}
override fun onBindViewHolder(holder: SimpleViewHolder, position: Int, context: Context, payload: Any) {
holder.textView.text = item.title
}
}
3. Create cell(s) and add them to the SimpleRecyclerView
List<Book> books = DataUtils.getBooks();
List<BookCell> cells = new ArrayList<>();
for (Book book : books) {
BookCell cell = new BookCell(book);
// There are two default cell listeners: OnCellClickListener<T> and OnCellLongClickListener<T>
cell.setOnCellClickListener(new SimpleCell.OnCellClickListener<Book>() {
@Override
public void onCellClicked(Book item) {
...
}
});
cell.setOnCellLongClickListener(new SimpleCell.OnCellLongClickListener<Book>() {
@Override
public void onCellLongClicked(Book item) {
...
}
});
cells.add(cell);
}
simpleRecyclerView.addCells(cells);
Then, there you go!
<a name=multi_types>Multiple Types</a>
SimpleRecyclerView supports multiple cell types. You can add different type of cells to it just like adding different type of objects to a list. The SimpleRecyclerView will handle the rest for you.
List<Book> books = DataUtils.getBooks();
List<Ad> ads = DataUtils.getAds();
List<SimpleCell> cells = new ArrayList<>();
for (Book book : books) {
BookCell cell = new BookCell(book);
cells.add(cell);
}
for (Ad ad : ads) {
BookAdCell cell = new BookAdCell(ad);
int position = (ads.indexOf(ad) + 1) * 3;
// take up full row
cell.setSpanSize(simpleRecyclerView.getGridSpanCount());
cells.add(position, cell);
}
simpleRecyclerView.addCells(cells);
<a name=cell_ops>Cell Operations</a>
SimpleRecyclerView provides basic CRUD cell operations.
It is common that loading cache data first and then fetch new data from network to update the list. The library provides addOrUpdateCell() and addOrUpdateCells() operation to achieve that (It uses [DiffUtils][1] under the hood). The cells will not be updated (i.e. receive onBindViewHolder() callback) if their bounded data models are the same, otherwsie they will be added to the end of list.
To enable this feature, the cells must be implemented Updatable interface.
public interface Updatable<T> {
boolean areContentsTheSame(T newItem);
Object getChangePayload(T newItem);
}
public class BookCell extends SimpleCell<Book, BookCell.ViewHolder>
implements Updatable<Book> {
...
@Override
protected void onBindViewHolder(ViewHolder holder, int position, Context context, Object payload) {
if (payload != null) {
// partial update
if (payload instanceof Bundle) {
Bundle bundle = ((Bundle) payload);
for (String key : bundle.keySet()) {
if (KEY_TITLE.equals(key)) {
holder.textView.setText(bundle.getString(key));
}
}
}
return;
}
...
}
/**
* If the titles of books are same, no need to update the cell, onBindViewHolder() will not be called.
*/
@Override
public boolean areContentsTheSame(Book newItem) {
return getItem().getTitle().equals(newItem.getTitle());
}
/**
* If getItem() is the same as newItem (i.e. their return value of getItemId() are the same)
* and areContentsTheSame() return false, then the cell need to be updated,
* onBindViewHolder() will be called with this payload object.
* */
@Override
public Object getChangePayload(Book newItem) {
Bundle bundle = new Bundle();
bundle.putString(KEY_TITLE, newItem.getTitle());
return bundle;
}
...
}
<a name=divider>Divider</a>
<com.jaychang.srv.SimpleRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srv_layoutMode="linearVertical"
app:srv_showDivider="true|false"
app:srv_showLastDivider="true|false"
app:srv_dividerOrientation="vertical|horizontal|both"
app:srv_dividerColor="@color/your_color"
app:srv_dividerPaddingLeft="dp"
app:srv_dividerPaddingRight="dp"
app:srv_dividerPaddingTop="dp"
app:srv_dividerPaddingBottom="dp" />
<a name=spacing>Spacing</a>
<com.jaychang.srv.SimpleRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srv_layoutMode="linearVertical"
app:srv_spacing="dp"
app:srv_verticalSpacing="dp"
app:srv_horizontalSpacing="dp"
app:srv_isSpacingIncludeEdge="true|false" />
<a name=empty_view>Empty State View</a>
The empty state view will be shown automatically when there is no data. If you want to show the empty state view explicitly, you can set srv_showEmptyStateView attribute to true (Default false).
<com.jaychang.srv.SimpleRecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:srv_layoutMode="linearVertical"
app:srv_emptyStateView="@layout/view_empty_state"
app:srv_showEmptyStateView="true|false" />
<a name=section_header>Section Header</a>
You can group cells together by providing a [SectionHeaderProvider<T>][2] to setSectionHeader(provider). A shorthand method [SimpleSectionHeaderProvider<T>][3] is also provided. The section header is not interative (e.g. can't be clicked)
SectionHeaderProvider<Book> sectionHeaderProvider = new SimpleSectionHeaderProvider<Book>() {
// Your section header view here
@NonNull
@Override
public View getSectionHeaderView(Book item, int position) {
View view = LayoutInflater.from(SectionHeaderActivity.this).inflate(R.layout.view_section_header, null, false);
TextView textView = (TextView) view.findViewById(R.id.textView);
textView.setText(String.format(getString(R.string.category), item.getCategoryName()));
return view;
}
// Your grouping logic here
@Override
public boolean isSameSection(Book item, Book nextItem) {
return item.getCategoryId() == nextItem.getCategoryId();
}
// Optional, whether th
