Skip to content

Instantly share code, notes, and snippets.

@gibson-khs
Last active November 28, 2017 08:30
Show Gist options
  • Select an option

  • Save gibson-khs/04a7ae2cb538451e187f9c0e3d1359ac to your computer and use it in GitHub Desktop.

Select an option

Save gibson-khs/04a7ae2cb538451e187f9c0e3d1359ac to your computer and use it in GitHub Desktop.
public abstract class BaseAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
protected final int VIEW_TYPE_HEADER = 0;
protected final int VIEW_TYPE_ITEM = 1;
protected final int VIEW_TYPE_FOOTER = 2;
protected Context context;
protected ArrayList<T> items = new ArrayList<>();
protected int headerViewCount = 0;
protected int footerViewCount = 0;
private OnLoadMoreListener onLoadMoreListener;
protected boolean isLoading;
private boolean isLastPage = true;
private int itemPage = 1;
public BaseAdapter(Context context) {
this.context = context;
}
public void resetAll(ArrayList<T> items) {
this.items.clear();
this.items.addAll(items);
notifyDataSetChanged();
}
public void addAll(ArrayList<T> items) {
this.items.addAll(items);
notifyDataSetChanged();
}
protected View getFooterView(ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.card_listview_footer, parent, false);
}
public void setHeaderViewCount(int count) {
headerViewCount = count;
}
public void setFooterViewCount(int footerViewCount) {
this.footerViewCount = footerViewCount;
}
@Override
public int getItemCount() {
return items.size() + headerViewCount + footerViewCount;
}
protected int getFooterPosition() {
return getItemCount() - 1;
}
public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) {
this.onLoadMoreListener = onLoadMoreListener;
}
protected void setLoadMore(int position) {
if (onLoadMoreListener != null && position == getFooterPosition() && !isLoading && !isLastPage) {
isLoading = true;
itemPage++;
onLoadMoreListener.onLoadMore(itemPage);
DLog.d("onLoadMore");
}
}
public void setLoaded() {
isLoading = false;
}
public void setLastPage(boolean isLastPage) {
this.isLastPage = isLastPage;
if (isLastPage)
itemPage = 1;
}
}
public interface OnLoadMoreListener {
void onLoadMore(int page);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment