MultiType 是一個(gè)分發(fā)管理類,幫助我們輕松實(shí)現(xiàn)復(fù)雜布局.建議大家閱讀源碼,作者的思路并不復(fù)雜但很巧妙.
下面附上主要源碼:
public class LoadMoreDelegate {
private Items mItems;
private MultiTypeAdapter mMultiTypeAdapter;
private OnLoadMoreListener mOnLoadMoreListener;
private ScrollListener mScrollListener;
public LoadMoreDelegate(MultiTypeAdapter multiTypeAdapter, Items items, OnLoadMoreListener onLoadMoreListener) {
mMultiTypeAdapter = multiTypeAdapter;
mItems = items;
this.mOnLoadMoreListener = onLoadMoreListener;
}
public void attach(RecyclerView recyclerView) {
LinearLayoutManager linearLayoutManager = (LinearLayoutManager) recyclerView.getLayoutManager();
mScrollListener = new ScrollListener(mMultiTypeAdapter, mItems, linearLayoutManager, mOnLoadMoreListener);
recyclerView.addOnScrollListener(mScrollListener);
}
public void loadMoreComplete() {
mScrollListener.setLoading(false);
}
public void addData(Items items) {
int originSize = mItems.size() - 1;
mItems.remove(originSize);//刪除"加載更多"
mItems.addAll(items);//添加新數(shù)據(jù)
//從最后的位置插入新數(shù)據(jù)
mMultiTypeAdapter.notifyItemRangeInserted(originSize, items.size() - 1);
}
public interface OnLoadMoreListener {
void onLoadMore();
}
private static class ScrollListener extends RecyclerView.OnScrollListener {
private final int size = 2;
private boolean mLoading = false;
private Items mItems;
private final LoadMore mLoadMore = new LoadMore();
private LinearLayoutManager mLinearLayoutManager;
private OnLoadMoreListener mOnLoadMoreListener;
private MultiTypeAdapter mMultiTypeAdapter;
ScrollListener(MultiTypeAdapter multiTypeAdapter, Items datas, LinearLayoutManager linearLayoutManager, OnLoadMoreListener onLoadMoreListener) {
mItems = datas;
mMultiTypeAdapter = multiTypeAdapter;
this.mLinearLayoutManager = linearLayoutManager;
this.mOnLoadMoreListener = onLoadMoreListener;
}
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
if (dy < 0) {//下滑忽略
return;
}
int totalNum = mLinearLayoutManager.getItemCount();
int lastVisiblePosition = mLinearLayoutManager.findLastCompletelyVisibleItemPosition();
if (!mLoading && lastVisiblePosition == totalNum - size) {//最后可見(jiàn)的view的位置為倒數(shù)第size個(gè),觸發(fā)加載更多
mLoading = true;
mItems.add(mLoadMore);
mMultiTypeAdapter.notifyItemInserted(mItems.size() - 1);
mOnLoadMoreListener.onLoadMore();
}
}
void setLoading(boolean loading) {
this.mLoading = loading;
}
}
}
這里提下有些加載更多只是簡(jiǎn)單的直接使用 notifyDataSetChanged() 方法,效率上我沒(méi)有研究過(guò),不敢說(shuō)哪個(gè)效率更好,但至少 RecycleView 給我們提供的動(dòng)畫效果沒(méi)有了,所以建議還是采用 notifyItemInserted().
完整示例:multitypeloadmore
public class PullUpTestActivity extends AppCompatActivity implements LoadMoreDelegate.OnLoadMoreListener {
private Items mItems = new Items();
private LoadMoreDelegate mLoadMoreDelegate;
@Bind(R.id.show)
RecyclerView mShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pull_up);
ButterKnife.bind(this);
for (int i = 0; i < 20; i++) {
mItems.add("data***" + i);
}
MultiTypeAdapter multiTypeAdapter = new MultiTypeAdapter(mItems);
multiTypeAdapter.register(String.class, new NormalViewProvider());
multiTypeAdapter.register(LoadMore.class, new LoadMoreViewProvider());
mShow.setLayoutManager(new LinearLayoutManager(this));
mShow.setAdapter(multiTypeAdapter);
mLoadMoreDelegate = new LoadMoreDelegate(multiTypeAdapter, mItems, this);
mLoadMoreDelegate.attach(mShow);
}
@Override
public void onLoadMore() {
getMore();
}
private void getMore() {
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(2000);
runOnUiThread(new Runnable() {
@Override
public void run() {
Items strings = new Items();
for (int i = 0; i < 20; i++) {
strings.add("data***more" + i);
}
mLoadMoreDelegate.addData(strings);
mLoadMoreDelegate.loadMoreComplete();
}
});
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}).start();
}
}
還有一點(diǎn)提一下,這樣的必須需要滑動(dòng)界面才可以觸發(fā)加載更多,如果第一屏數(shù)據(jù)不超過(guò)一屏,將沒(méi)法觸發(fā)加載更多.但這樣我認(rèn)為是合理的,如果第一頁(yè)數(shù)據(jù)的請(qǐng)求就不滿一屏,所以后面應(yīng)該是在沒(méi)有數(shù)據(jù)的.有些加載更多的實(shí)現(xiàn)是一到最后一個(gè)數(shù)據(jù),就開始加載更多.但在第一頁(yè)數(shù)據(jù)不滿一屏的情況下,觸發(fā)了加載很多,很違背直覺(jué). BaseRecyclerViewAdapterHelper 就采用了這種實(shí)現(xiàn),大家可以去看源碼. 而且一般第一頁(yè)的請(qǐng)求,我們一般會(huì)有自己的一個(gè) loading 視圖,這樣不滿一屏的情況下,還有一個(gè)加載更多的視圖,這是不太合理的.
以上 demo 僅供參考,大家可以進(jìn)一步自己封裝和實(shí)現(xiàn).可以自己實(shí)現(xiàn)個(gè) MultiTypeAdapter 并把代碼整合到里面去,這樣用起來(lái)可能會(huì)更方便點(diǎn).
效果圖