RecylerView
上拉加載更多
上拉加載的多狀態(tài)
這篇博客是承接上一篇博客--探索Android架構(gòu)的DataLayer層(DataManager方式)具體實現(xiàn)抵乓,其實是上篇博客的一個使用比較普遍的例子扯俱,當(dāng)然如果把上一篇博客設(shè)計的數(shù)據(jù)加載回調(diào)接口提煉出來也是可以做一篇單獨的文章灼芭。
先說說我們希望的RecycerView應(yīng)該有的樣子:上拉加載更多,沒有更多旦事,加載錯誤然后點擊重試。至于通常的下拉刷新我同意Google設(shè)計下拉刷新的理念,即下拉刷新是View的行為卷仑,應(yīng)該讓View自己來實現(xiàn),而下拉刷新有著更加廣泛的應(yīng)用麸折,幾乎需要交互的信息顯示界面都是需要下拉刷新功能的锡凝。所以RecyclerView的下拉刷新應(yīng)該由她的父空間來實現(xiàn),有過有這個需求的話垢啼,因為也不是一定需要下拉刷新窜锯。這里下拉刷新控件我推薦秋百萬的下拉刷新控件,6k+的star芭析,值得信賴锚扎。
先談?wù)勊悸罚鋵嵑芎唵文倨簦褪峭ㄟ^getItemViewType()加載不同的布局驾孔,這里就是把加載更多布局封裝進(jìn)adapter中。
接著上篇寫的數(shù)據(jù)加載接口來看惯疙,開始加載的時候加入加載的itemView翠勉,完成加載后去掉它。通過給RecyclerView添加的滑動事件來判斷加載時機(jī)霉颠。先看看上篇博客寫的數(shù)據(jù)加載接口吧:
public interface DataLoadingSubject {
boolean isDataLoading();
void registerCallback(DataLoadingCallbacks callBack);
void unregistereCallBack(DataLoadingCallbacks callBack);
interface DataLoadingCallbacks{
void dataStartedLoading();
void dataFinishedLoading();
}
}
接下來我們定義加載更多的itemViewType的值是-1对碌,直接讓adpater實現(xiàn)上面這個接口,這里直接貼出adapter的代碼:
public class ArticleAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
implements DataLoadingSubject.DataLoadingCallbacks {
private static final int TYPE_LOADING_MORE = -1;
private static final int TYPE_ARTICLE_T1 = 0;
//用來加載更多視圖樣板
//private static final int TYPE_ARTICLE_T2 = 1;
//private static final int TYPE_ARTICLE_T3 = 2;
private List<Article> mArticles;
private Activity mActivity;
private LayoutInflater mLayoutInflater;
private DataLoadingSubject dataLoading;
private boolean showLoadingMore = false;
public ArticleAdapter(Activity activity,
DataLoadingSubject dataLoading) {
this.mActivity = activity;
this.dataLoading = dataLoading;
dataLoading.registerCallback(this);
mLayoutInflater = mActivity.getLayoutInflater();
mArticles = new ArrayList<>();
}
@Override
public int getItemViewType(int position) {
if (position < getDataItemCount()
&& getDataItemCount() > 0) {
return TYPE_ARTICLE_T1;
}
return TYPE_LOADING_MORE;
}
@Override
public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
switch (viewType){
case TYPE_LOADING_MORE:
return new LoadingMoreHolder(mLayoutInflater.inflate(R.layout.infinite_loading,parent,false));
case TYPE_ARTICLE_T1:
return new ArticleT1Holder(mLayoutInflater.inflate(R.layout.item_articles_t1,parent,false));
}
return null;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
switch (getItemViewType(position)){
case TYPE_LOADING_MORE:
bindLoadingViewHolder((LoadingMoreHolder) holder,position);
break;
case TYPE_ARTICLE_T1:
bindActicleT1Holder(getItem(position), (ArticleT1Holder) holder);
break;
}
}
private Article getItem(int position){
return mArticles.get(position);
}
public int getDataItemCount() {
return mArticles.size();
}
@Override
public int getItemCount() {
return getDataItemCount() + (showLoadingMore ? 1 : 0);
}
@Override
public void dataStartedLoading() {
if (showLoadingMore) return;
showLoadingMore = true;
notifyItemInserted(getLoadingMoreItemPosition());
}
@Override
public void dataFinishedLoading() {
if (showLoadingMore) return;
int loadingPos = getLoadingMoreItemPosition();
showLoadingMore = false;
notifyItemRemoved(loadingPos);
}
private void bindActicleT1Holder(Article article,ArticleT1Holder holder){
Glide.with(mActivity)
.load(article.HeadImgId)
.diskCacheStrategy(DiskCacheStrategy.RESULT)
.fitCenter()
.into(holder.image);
holder.abstractTv.setText(article.Abstract);
holder.titleTv.setText(article.Title);
}
private void bindLoadingViewHolder(LoadingMoreHolder hodler, int position) {
hodler.mProgressBar.setVisibility((position > 0 && dataLoading.isDataLoading())
? View.VISIBLE : View.INVISIBLE);
}
public int getLoadingMoreItemPosition() {
return showLoadingMore ? getItemCount() - 1 : RecyclerView.NO_POSITION;
}
static class ArticleT1Holder extends RecyclerView.ViewHolder {
@BindView(R.id.foregounde_iv)
ImageView image;
@BindView(R.id.title_tv)
TextView titleTv;
@BindView(R.id.abstract_tv)
TextView abstractTv;
public ArticleT1Holder(View itemView) {
super(itemView);
ButterKnife.bind(this, itemView);
}
}
static class LoadingMoreHolder extends RecyclerView.ViewHolder {
ProgressBar mProgressBar;
public LoadingMoreHolder(View itemView) {
super(itemView);
mProgressBar = (ProgressBar) itemView;
}
}
}
然后我們開始判斷這個滑動到底的時候加載更多蒿偎,這個時候我們給我們的RecyclerView添加一個滑動事件監(jiān)聽事件:
public abstract class InfiniteScrollListener extends RecyclerView.OnScrollListener {
// 底部還剩下幾個的時候開始觸發(fā)加載更多的回調(diào)接口
private static final int VISIBLE_THRESHOLD = 5;
private final LinearLayoutManager layoutManager;
private final DataLoadingSubject dataLoading;
public InfiniteScrollListener(@NonNull LinearLayoutManager layoutManager,
@NonNull DataLoadingSubject dataLoading) {
this.layoutManager = layoutManager;
this.dataLoading = dataLoading;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
// bail out if scrolling upward or already loading data
if (dy < 0 || dataLoading.isDataLoading()) return;
final int visibleItemCount = recyclerView.getChildCount();
final int totalItemCount = layoutManager.getItemCount();
final int firstVisibleItem = layoutManager.findFirstVisibleItemPosition();
if ((totalItemCount - visibleItemCount) <= (firstVisibleItem + VISIBLE_THRESHOLD)) {
onLoadMore();
}
}
public abstract void onLoadMore();
}
最后當(dāng)然是在presenter或者activity中如何使用俭缓,我今天寫的這個其實是上一篇博客的繼續(xù)或者例子。
mDataManager = new ArticleDataManager(this, "870a9973-e657-401d-a12b-0da036a29583") {
@Override
public void onDataLoaded(List<Article> data) {
mAdapter.addDataSet(data);
}
};
mAdapter = new ArticleAdapter(this, mDataManager);
mRecyclerView.setAdapter(mAdapter);
final LinearLayoutManager layoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(layoutManager);
mRecyclerView.addOnScrollListener(new InfiniteScrollListener(layoutManager, mDataManager) {
@Override
public void onLoadMore() {
mDataManager.loadData();
}
});
mDataManager.loadData();
OK酥郭,因為網(wǎng)速太好經(jīng)郴梗看不到那個加載視圖就不截圖了,需要的自己試試吧不从,原理也不復(fù)雜惜姐,不用框架自己嘗試也是蠻好的。