Skip to content

Commit

Permalink
Merge pull request #11 from githubzaibao/master
Browse files Browse the repository at this point in the history
修改部分接口及修改了PullToRefreshUseActivity 实例程序里的代码,使用新的api
  • Loading branch information
CymChad committed May 3, 2016
2 parents 2e0fe47 + bfdebf2 commit a53b95a
Show file tree
Hide file tree
Showing 2 changed files with 112 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ public void onLoadMoreRequested() {
mRecyclerView.post(new Runnable() {
@Override
public void run() {
mQuickAdapter.isNextLoad(false);
// mQuickAdapter.isNextLoad(false);
mQuickAdapter.notifyDataChangedAfterLoadMore(false);
}
});

Expand All @@ -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);
Expand All @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;


Expand All @@ -31,7 +32,7 @@
*/
public abstract class BaseQuickAdapter<T> extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

private boolean mNextLoadEnable;
private boolean mNextLoadEnable = false;
private boolean mLoadingMoreEnable = false;
private boolean mFirstOnlyEnable = true;
private boolean mOpenAnimationEnable = false;
Expand Down Expand Up @@ -92,23 +93,71 @@ public abstract class BaseQuickAdapter<T> extends RecyclerView.Adapter<RecyclerV
protected static final int EMPTY_VIEW = 0x00000555;
private View mHeaderView;
private View mFooterView;
private int pageSize = -1;
/**
* View to show if there are no items to show.
*/
private View mEmptyView;

/**
* call the method will not enable the loadMore funcation and the params pageSize is invalid
* more infomation see{@link public void openLoadMore(int pageSize, boolean enable),@link public void setOnLoadMoreListener(RequestLoadMoreListener requestLoadMoreListener)} method
*
* @param pageSize
* @param requestLoadMoreListener
*/
@Deprecated
public void setOnLoadMoreListener(int pageSize, RequestLoadMoreListener requestLoadMoreListener) {

setOnLoadMoreListener(requestLoadMoreListener);
}

public void setOnLoadMoreListener(RequestLoadMoreListener requestLoadMoreListener) {

mNextLoadEnable = true;
this.mRequestLoadMoreListener = requestLoadMoreListener;
}

/**
* when adapter's data size than pageSize and enable is true,the loading more function is enable,or disable
*
* @param pageSize
* @param enable
*/
public void openLoadMore(int pageSize, boolean enable) {
this.pageSize = pageSize;
mNextLoadEnable = enable;

}

/**
* call the method before you should call setPageSize() method to setting up the enablePagerSize value,whether it will invalid
* enable the loading more data function if enable's value is true,or disable
*
* @param enable
*/
public void openLoadMore(boolean enable) {
mNextLoadEnable = enable;

}

/**
* setting up the size to decide the loading more data funcation whether enable
* enable if the data size than pageSize,or diable
*
* @param pageSize
*/
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}

/**
* return the value of pageSize
*
* @return
*/
public int getPageSize() {
return this.pageSize;
}

public void setOnRecyclerViewItemClickListener(OnRecyclerViewItemClickListener onRecyclerViewItemClickListener) {
this.onRecyclerViewItemClickListener = onRecyclerViewItemClickListener;
}
Expand Down Expand Up @@ -149,12 +198,17 @@ public void onClick(View v) {
public BaseQuickAdapter(Context context, int layoutResId, List<T> data) {
this.mData = data == null ? new ArrayList<T>() : new ArrayList<T>(data);
this.mContext = context;
this.mLayoutResId = layoutResId;
if (layoutResId != 0) {
this.mLayoutResId = layoutResId;
}
}

public BaseQuickAdapter(Context context, List<T> data) {
this.mData = data == null ? new ArrayList<T>() : new ArrayList<T>(data);
this.mContext = context;
this(context, 0, data);
}

public BaseQuickAdapter(Context context) {
this(context, null);
}

public void remove(int position) {
Expand All @@ -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<? extends T> 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<T> data) {
this.mData = data;
notifyDataSetChanged();
}


public List getData() {
return mData;
}
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -315,13 +395,26 @@ 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;
notifyDataSetChanged();

}

public void notifyDataChangedAfterLoadMore(boolean isNextLoad) {
mNextLoadEnable = isNextLoad;
mLoadingMoreEnable = false;
notifyDataSetChanged();

}


@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
Expand Down Expand Up @@ -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) {
Expand All @@ -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);
Expand Down

0 comments on commit a53b95a

Please sign in to comment.