Skip to content

Instantly share code, notes, and snippets.

@chowaikong
Created April 4, 2017 03:04
Show Gist options
  • Save chowaikong/b099048dd681dfdb1b347f2cea6ce4f9 to your computer and use it in GitHub Desktop.
Save chowaikong/b099048dd681dfdb1b347f2cea6ce4f9 to your computer and use it in GitHub Desktop.
public abstract class BaseRVAdapter<T extends ViewDataBinding, M>
extends RecyclerView.Adapter<DataBindingViewHolder<T>> {
List<M> mDataList;
public BaseRVAdapter(List<M> mDataList) {
this.mDataList = mDataList;
}
@Override public int getItemCount() {
return mDataList != null ? mDataList.size() : 0;
}
public void setData(List<M> dataList) {
if (this.mDataList != null && dataList != null) {
if (this.mDataList.isEmpty()) {
this.mDataList.clear();
this.mDataList.addAll(dataList);
notifyItemRangeInserted(0, dataList.size());
} else {
this.mDataList.clear();
this.mDataList.addAll(dataList);
notifyDataSetChanged();
}
}
}
public void addData(List<M> dataList) {
if (this.mDataList != null && dataList != null) {
int size = this.mDataList.size();
this.mDataList.addAll(dataList);
notifyItemRangeInserted(size, dataList.size());
}
}
public M removeData(int position) {
M obj = null;
if (this.mDataList != null && this.mDataList.size() > position && position >= 0) {
obj = mDataList.remove(position);
}
notifyItemRemoved(position);
return obj;
}
public List<M> getDataList() {
return mDataList;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment