A RecyclerView extension for building list more easily.

Min SDK   14
Latest Commit   2017-09-22 16:21:06
License   Apache-2.0

Github   https://github.com/jaychang0917/SimpleRecyclerView

         

标签

使用APP客户端扫描二维码运行Demo


...

jaychang0917

Hong Kong


         

类似的还有....
RecyclerViewWithHeaderNewPractice A new implement of RecyclerView with HeaderViews
LRecyclerView RecyclerView下拉刷新,自动加载更多;仿IOS侧滑Item删除菜单
SuspensionBar A RecyclerView suspension bar implementation like Instagram
README

README.md

SimpleRecyclerView

Release Android Weekly

A RecyclerView extension for building list more easily.

Screenshot

Table of Contents

Sample Project

Sample apk

Installation

In your project level build.gradle :

allprojects {
    repositories {
        ...
        maven { url "https://jitpack.io" }
    }
}

In your app level build.gradle :

dependencies {
    compile 'com.github.jaychang0917:SimpleRecyclerView:{latest_version}'
}

Release


Basic Usage

Basically, there are three steps to build your list.

1. Configure the SimpleRecyclerView

Full attributes list

<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, List<Object> payloads) {
    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 {
    @BindView(R.id.textView)
    TextView textView;

    ViewHolder(View itemView) {
      super(itemView);
      ButterKnife.bind(this, itemView);
    }
  }

}
Kotlin android extension support

If you are using Kotlin android extension, just one line of code for creating your ViewHolder

class ViewHolder(itemView: View) : SimpleViewHolder(itemView)

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<CELL, VH, T> and OnCellLongClickListener<CELL, VH, 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!

Multiple Types

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);

Cell Operations

SimpleRecyclerView provides basic CRUD cell operations.

Full cell operations list

It is common that loading cache data first and then fetch new data from network to update the list. The library provides addOrUpdateCell() and