diff --git a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/PullToRefreshUseActivity.java b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/PullToRefreshUseActivity.java index bf0e0a8bf..ddcf0efa0 100644 --- a/app/src/main/java/com/chad/baserecyclerviewadapterhelper/PullToRefreshUseActivity.java +++ b/app/src/main/java/com/chad/baserecyclerviewadapterhelper/PullToRefreshUseActivity.java @@ -63,7 +63,8 @@ public void onLoadMoreRequested() { mRecyclerView.post(new Runnable() { @Override public void run() { - mQuickAdapter.isNextLoad(false); + // mQuickAdapter.isNextLoad(false); + mQuickAdapter.notifyDataChangedAfterLoadMore(false); } }); @@ -73,7 +74,8 @@ public void run() { public void run() { DataServer.addData(mQuickAdapter.getData(), PAGE_SIZE); mCurrentCounter = mQuickAdapter.getItemCount(); - mQuickAdapter.isNextLoad(true); + // mQuickAdapter.isNextLoad(true); + mQuickAdapter.notifyDataChangedAfterLoadMore(true); } }, delayMillis); @@ -97,7 +99,9 @@ private void initAdapter() { mQuickAdapter.openLoadAnimation(); mRecyclerView.setAdapter(mQuickAdapter); mCurrentCounter = mQuickAdapter.getItemCount(); - mQuickAdapter.setOnLoadMoreListener(PAGE_SIZE, this); + // mQuickAdapter.setOnLoadMoreListener(PAGE_SIZE, this); + mQuickAdapter.setOnLoadMoreListener(this); + mQuickAdapter.openLoadMore(PAGE_SIZE,true);//or call mQuickAdapter.setPageSize(PAGE_SIZE); mQuickAdapter.openLoadMore(true); addHeadView(); mQuickAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() { @Override diff --git a/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java b/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java index 9218c1416..f23349d0a 100755 --- a/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java +++ b/library/src/main/java/com/chad/library/adapter/base/BaseQuickAdapter.java @@ -23,6 +23,7 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.util.ArrayList; +import java.util.Collection; import java.util.List; @@ -31,7 +32,7 @@ */ public abstract class BaseQuickAdapter extends RecyclerView.Adapter { - private boolean mNextLoadEnable; + private boolean mNextLoadEnable = false; private boolean mLoadingMoreEnable = false; private boolean mFirstOnlyEnable = true; private boolean mOpenAnimationEnable = false; @@ -92,11 +93,19 @@ public abstract class BaseQuickAdapter extends RecyclerView.Adapter data) { this.mData = data == null ? new ArrayList() : new ArrayList(data); this.mContext = context; - this.mLayoutResId = layoutResId; + if (layoutResId != 0) { + this.mLayoutResId = layoutResId; + } } public BaseQuickAdapter(Context context, List data) { - this.mData = data == null ? new ArrayList() : new ArrayList(data); - this.mContext = context; + this(context, 0, data); + } + + public BaseQuickAdapter(Context context) { + this(context, null); } public void remove(int position) { @@ -168,6 +222,32 @@ public void add(int position, T item) { notifyItemInserted(position); } + /** + * After clear the adapter's data, add new collection + * + * @param collection + */ + public void addAfterClear(Collection collection) { + try { + mData.clear(); + mData.addAll(collection); + notifyDataSetChanged(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + /** + * setting up a new instance to data; + * + * @param data + */ + public void setNewData(List data) { + this.mData = data; + notifyDataSetChanged(); + } + + public List getData() { return mData; } @@ -199,7 +279,7 @@ public int getmEmptyViewCount() { @Override public int getItemCount() { - int i = mNextLoadEnable ? 1 : 0; + int i = isLoadMore() ? 1 : 0; int count = mData.size() + i + getHeaderViewsCount() + getFooterViewsCount(); mEmptyEnable = false; if (count == 0) { @@ -315,6 +395,12 @@ public View getEmptyView() { return mEmptyView; } + /** + * see more {@link public void notifyDataChangedAfterLoadMore(boolean isNextLoad)} + * + * @param isNextLoad + */ + @Deprecated public void isNextLoad(boolean isNextLoad) { mNextLoadEnable = isNextLoad; mLoadingMoreEnable = false; @@ -322,6 +408,13 @@ public void isNextLoad(boolean isNextLoad) { } + public void notifyDataChangedAfterLoadMore(boolean isNextLoad) { + mNextLoadEnable = isNextLoad; + mLoadingMoreEnable = false; + notifyDataSetChanged(); + + } + @Override public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) { @@ -354,7 +447,7 @@ public void onClick(View v) { } } } else if (holder instanceof FooterViewHolder) { - if (mNextLoadEnable && !mLoadingMoreEnable && mRequestLoadMoreListener != null) { + if (isLoadMore()) { mLoadingMoreEnable = true; mRequestLoadMoreListener.onLoadMoreRequested(); if (holder.itemView.getLayoutParams() instanceof StaggeredGridLayoutManager.LayoutParams) { @@ -374,6 +467,10 @@ public void onClick(View v) { } } + private boolean isLoadMore() { + return mNextLoadEnable && pageSize != -1 && !mLoadingMoreEnable && mRequestLoadMoreListener != null && mData.size() >= pageSize; + } + protected View getItemView(int layoutResId, ViewGroup parent) { return LayoutInflater.from(parent.getContext()).inflate( layoutResId, parent, false);