Skip to content

Commit

Permalink
* Updated README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
longbkiter07 committed Apr 24, 2017
1 parent 65fe2d3 commit e8e9024
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 244 deletions.
45 changes: 24 additions & 21 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# SnappyRecyclerAdapter

This library helps you to manage `RecyclerViewAdapter` easier.
This library helps you to manage `RecyclerViewAdapter` easier. This library is very useful for rendering a sorted data in real time (For example: chat application)

# Features

* Calling `notifyDataSetChanges`, `notifyItemInserted`, `notifyItemChanged`... and apply [DiffUtil] (https://developer.android.com/reference/android/support/v7/util/DiffUtil.html) automatically so you don't care about them anymore.
* Calling `DiffUtil` in background thread so that the ui does not lag.
* Calling `notifyDataSetChanges`, `notifyItemInserted`, `notifyItemChanged`... automatically
* Applying both `SortedList` and `DiffUtil`
* Calling `DiffUtil` in background thread so that the ui does not lag
* Queueing events to keep data is consistent.
* Observable method so that we can catch the callback to do next action.
* Supporting LinkedList, ArrayList for reasonable usage.

# Usage

Expand All @@ -25,36 +25,39 @@ compile 'com.android.support:recyclerview-v7:24.+'
```

Code example: read [here] (https://github.com/longbkiter07/SnappyRecyclerAdapter/blob/master/app/src/main/java/com/silong/snappyrecycleradapter/adapter/UserRecyclerViewAdapter.java)
Code example: please read sample app

## Create your RecyclerList:

```
List<T> recyclerList = new ArrayList<>();
```
Or
```
List<T> recyclerList = new RecyclerLinkedList<>();
```
`RecyclerLinkedList` is a list that helps you to improve performance for `get(int index)` method.
## Create your RecyclerViewAdapter:

```
public class MyAdapter<VH> extends RecyclerViewAdapter<VH> {
private final ObservableAdapterManager<User> mObservableAdapterManager;
public MyRecyclerViewAdapter(List<User> items) {
mObservableAdapterManager = new ObservableAdapterManager<User>(this, items, null);
private final RxSortedList<T> mRxSortedList;
public MyRecyclerViewAdapter() {
mRxSortedList = new RxSortedList<>(T.class, new RxRecyclerViewCallback<T>(this) {
@Override
public boolean areContentsTheSame(T oldData, T newData) {
return //return whether content of oldData and newData are the same
}
@Override
public boolean areItemsTheSame(T oldData, T newData) {
return //return whether oldData and newData are the same (checking object id is recommended)
}
@Override
public int compare(T o1, T o2) {
return //sort order.
}
});
}
@Override
public int getItemCount() {
return mObservableAdapterManager.getItemCount();
return mRxSortedList.size();
}
...
}
```

If you want to use `DiffUtil`, you would implement `DataComparable` to define differences between 2 items. Otherwise, you can pass `null` value.

## Add item:

```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ public RxRecyclerViewCallback(RecyclerView.Adapter adapter) {
mAdapter = adapter;
}


@Override
public void onChanged(int position, int count) {
mAdapter.notifyItemChanged(position, count);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Comparator;

/**
* The class that controls the behavior of the {@link android.support.v7.util.SortedList}.
* The class that controls the behavior of the {@link me.silong.snappyadapter.RxSortedList}.
* <p>
* It defines how items should be sorted and how duplicates should be handled.
* <p>
Expand Down Expand Up @@ -50,8 +50,7 @@ public void onChanged(int position, int count, Object payload) {
* so
* that you can change its behavior depending on your UI.
* <p>
* For example, if you are using SortedList with a {@link android.support.v7.widget.RecyclerView.Adapter
* RecyclerView.Adapter}, you should
* For example, if you are using SortedList with a RecyclerView.Adapter, you should
* return whether the items' visual representations are the same or not.
*
* @param oldItem The previous representation of the object.
Expand Down Expand Up @@ -84,10 +83,10 @@ public void onChanged(int position, int count, Object payload) {
* <p>
* If consecutive changes in the SortedList are not suitable for batching, BatchingCallback
* dispatches them as soon as such case is detected. After your edits on the SortedList is
* complete, you <b>must</b> always call {@link android.support.v7.util.SortedList.BatchedCallback#dispatchLastEvent()} to flush
* complete, you <b>must</b> always call {@link me.silong.snappyadapter.RxSortedListCallback.BatchedCallback#dispatchLastEvent()} to flush
* all changes to the Callback.
*/
public static class BatchedCallback<T2> extends RxSortedListCallback<T2> {
public static final class BatchedCallback<T2> extends RxSortedListCallback<T2> {

final RxSortedListCallback<T2> mWrappedCallback;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class SnappyDiffCallback<D> extends DiffUtil.Callback {
mNewDataLength = newDataLength;
}

public static <D> Pair<DiffUtil.DiffResult, D[]> calculate(RxSortedListCallback<D> callback, D[] oldData, D[] newData,
static <D> Pair<DiffUtil.DiffResult, D[]> calculate(RxSortedListCallback<D> callback, D[] oldData, D[] newData,
int oldDataLength, int newDataLength) {
return Pair.create(DiffUtil.calculateDiff(new SnappyDiffCallback<>(callback, oldData, newData, oldDataLength, newDataLength)), newData);
}
Expand Down
Loading

0 comments on commit e8e9024

Please sign in to comment.