Last active
April 27, 2019 03:16
-
-
Save tendoasan/f830121d5c3b0bedfd8f21c5943f7ca1 to your computer and use it in GitHub Desktop.
[Android-Util]#Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Created by Tendoasan on 2017/8/15. | |
* 基础分页适配器 | |
*/ | |
@SuppressWarnings("unused") | |
public abstract class BasePaginationAdapter<T> extends Adapter { | |
public interface PaginationListener { | |
void onLoadMoreItems(); | |
} | |
protected List<T> mItems; | |
private boolean mIsLoading; | |
private boolean mHasMoreItems; | |
private PaginationListener mPaginationListener; | |
private boolean mIsPaging; | |
private LinearLayoutManager mLayoutManger; | |
// 初始化 | |
public void init(RecyclerView rv, boolean isPaging) { | |
mIsPaging = isPaging; | |
mIsLoading = false; | |
if (rv.getLayoutManager() != null && rv.getLayoutManager() instanceof LinearLayoutManager){ | |
mLayoutManger = (LinearLayoutManager)rv.getLayoutManager(); | |
} | |
if(mIsPaging){ | |
// 底部加载更多 | |
rv.addOnScrollListener(new RecyclerView.OnScrollListener() { | |
@Override | |
public void onScrolled(RecyclerView recyclerView, int dx, int dy) { | |
super.onScrolled(recyclerView, dx, dy); | |
if(mLayoutManger == null){ | |
return; | |
} | |
int visibleItemCount = mLayoutManger.getChildCount(); | |
int totalItemCount = mLayoutManger.getItemCount(); | |
int firstVisibleItemPosition = mLayoutManger.findFirstVisibleItemPosition(); | |
// 不在加载+还有更多选项+底部 | |
if (!mIsLoading && mHasMoreItems | |
&& ((visibleItemCount + firstVisibleItemPosition) >= totalItemCount)) { | |
if (mPaginationListener != null) { | |
setLoading(true); | |
mPaginationListener.onLoadMoreItems(); | |
} | |
} | |
} | |
}); | |
} | |
} | |
public void onFinishLoading(boolean hasMoreItems) { | |
setLoading(false); | |
setHasMoreItems(hasMoreItems); | |
} | |
@Override | |
public int getItemViewType(int position) { | |
if(mItems != null && position == mItems.size()){ | |
return RecyclerView.INVALID_TYPE; | |
} | |
return super.getItemViewType(position); | |
} | |
public void clearData() { | |
if(mItems != null){ | |
mItems.clear(); | |
this.notifyDataSetChanged(); | |
} | |
} | |
@Override | |
public int getItemCount() { | |
return mItems == null ? 0 : mItems.size(); | |
//ic_dial_new is for loadingView | |
} | |
@Override | |
public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) { | |
return initViewHolder(viewGroup,viewType); | |
} | |
@Override | |
public void onBindViewHolder(ViewHolder viewHolder, int position) { | |
doBindViewHolder(viewHolder, position); | |
} | |
protected abstract ViewHolder initViewHolder(ViewGroup viewGroup, int viewType); | |
protected abstract void doBindViewHolder(ViewHolder viewHolder, int position); | |
public void setItems(List<T> items){ | |
mItems = items; | |
} | |
public List<T> getItems(){ | |
return mItems; | |
} | |
public boolean isLoading() { | |
return mIsLoading; | |
} | |
public void setLoading(boolean isLoading) { | |
mIsLoading = isLoading; | |
} | |
public void setHasMoreItems(boolean hasMoreItems) { | |
this.mHasMoreItems = hasMoreItems; | |
} | |
public boolean isHasMoreItems() { | |
return mHasMoreItems; | |
} | |
public void setPaginationListener(PaginationListener paginationListener) { | |
this.mPaginationListener = paginationListener; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment