I created a generic adapter to handle loadMore
public abstract class CommonModelAdapter<T,V extends BaseModelViewHolder<T>> extends RecyclerView.Adapter<V>{ public abstract V setViewHolder(ViewGroup parent); private Context mContext; private List<T> items; private List<T> copyItems; public static final int VIEW_TYPE_PROGRESS = 0; public static final int VIEW_TYPE_ITEM = 1; public CommonModelAdapter(Context mContext,List<T> items){ this.mContext = mContext; this.items = items; copyItems = new ArrayList<>(); copyItems.addAll(items); } @Override public V onCreateViewHolder(ViewGroup parent, int viewType) { if(viewType == VIEW_TYPE_ITEM){ return setViewHolder(parent); } else{ return (V) new ProgressViewHolder(parent ,R.layout.item_progress_loader); } } @Override public void onBindViewHolder(V holder, int position) { if (holder instanceof ProgressViewHolder) { ((ProgressViewHolder) holder).showProgressLoader(); } else{ try { holder.onBindData(items.get(position)); } catch (Exception e) { e.printStackTrace(); } } } @Override public int getItemCount() { return items.size(); } @Override public int getItemViewType(int position) { if(items.get(position) == null) return VIEW_TYPE_PROGRESS; else return VIEW_TYPE_ITEM; } public T getItemAt(int position){ return items.get(position); } public void setItems(List<T> newItems){ this.items = newItems; } }
Where is BaseModelViewHolder
public abstract class BaseModelViewHolder<T extends BaseModelBO> extends RecyclerView.ViewHolder { public abstract void onBindData(T data); public BaseModelViewHolder(ViewGroup parent, int layoutId) { super(LayoutInflater.from(parent.getContext()).inflate(layoutId, parent, false)); } public <T extends View> T findViewById(@IdRes int resId) { return (T) itemView.findViewById(resId); } }
LoadMoreRecyclerView
public class LoadMoreRecyclerView extends RecyclerView { private boolean loading = true; int pastVisiblesItems, visibleItemCount, totalItemCount; //WrapperLinearLayout is for handling crash in RecyclerView private WrapperLinearLayout mLayoutManager; private Context mContext; private OnLoadMoreListener onLoadMoreListener; public LoadMoreRecyclerView(Context context) { super(context); mContext = context; init(); } public LoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs) { super(context, attrs); mContext = context; init(); } public LoadMoreRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); mContext = context; init(); } private void init(){ mLayoutManager = new WrapperLinearLayout(mContext,LinearLayoutManager.VERTICAL,false); this.setLayoutManager(mLayoutManager); this.setItemAnimator(new DefaultItemAnimator()); this.setHasFixedSize(true); } @Override public void onScrolled(int dx, int dy) { super.onScrolled(dx, dy); if(dy > 0) //check for scroll down { visibleItemCount = mLayoutManager.getChildCount(); totalItemCount = mLayoutManager.getItemCount(); pastVisiblesItems = mLayoutManager.findFirstVisibleItemPosition(); if (loading) { if ( (visibleItemCount + pastVisiblesItems) >= totalItemCount) { loading = false; Log.v("...", "Call Load More !"); if(onLoadMoreListener != null){ onLoadMoreListener.onLoadMore(); } //Do pagination.. ie fetch new data } } } } @Override public void onScrollStateChanged(int state) { super.onScrollStateChanged(state); } public void enableLoadingMore(boolean moreLoading){ loading = moreLoading; } public void setOnLoadMoreListener(OnLoadMoreListener onLoadMoreListener) { this.onLoadMoreListener = onLoadMoreListener; } public void setScrolling(boolean enable){ mLayoutManager.setScrollEnabled(enable); } }
If you want to show progress ...
mLoadMoreRecyclerView.setOnLoadMoreListener(new OnLoadMoreListener() { @Override public void onLoadMore() { { overAllItems.add(null);
source share