一个支持RecyclerView加载更多、添加HeaderView的BaseAdapter
1. 添加JitPack仓库
在当前项目根目录下的 build.gradle
文件中添加如下内容:
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
2. 添加项目依赖
dependencies {
implementation 'com.github.SheHuan:RecyclerViewAdapter:1.2.9'
}
public class CommonRefreshAdapter extends CommonBaseAdapter<T> {
public CommonRefreshAdapter (Context context, List<T> datas, boolean isLoadMore) {
super(context, datas, isLoadMore);
}
@Override
protected void convert(ViewHolder holder, T data, int position) {
//init item
}
@Override
protected int getItemLayoutId() {
return item_layout;
}
}
public class MultiRefreshAdapter extends MultiBaseAdapter<T> {
public MultiRefreshAdapter(Context context, List<T> datas, boolean isOpenLoadMore) {
super(context, datas, isOpenLoadMore);
}
@Override
protected void convert(ViewHolder holder, T data, int position, int viewType) {
}
@Override
protected int getItemLayoutId(int viewType) {
}
@Override
protected int getViewType(int position, T data) {
}
}
CommonRefreshAdapter mAdapter = new CommonRefreshAdapter(this, data, true);
MultiRefreshAdapter mAdapter = new MultiRefreshAdapter(this, data, true);
PS:true代表开启加载更多。
View emptyView = LayoutInflater.from(this).inflate(R.layout.empty_layout, (ViewGroup) mRecyclerView.getParent(), false);
mAdapter.setEmptyView(emptyView);
mAdapter.removeEmptyView();
View reloadLayout = LayoutInflater.from(this).inflate(R.layout.reload_layout, (ViewGroup) mRecyclerView.getParent(), false);
mAdapter.setReloadView(reloadLayout);
mAdapter.setLoadingView(view);
mAdapter.setLoadFailedView(view);
mAdapter.setLoadEndView(view);
mAdapter.setOnLoadMoreListener(new OnLoadMoreListener() {
@Override
public void onLoadMore(boolean isReload) {
//do your load more;
}
});
mAdapter.setOnItemClickListener(new OnItemClickListeners<T>() {
@Override
public void onItemClick(ViewHolder viewHolder, T data, int position) {
}
});
mAdapter.setOnMultiItemClickListener(new OnMultiItemClickListeners<T>() {
@Override
public void onItemClick(ViewHolder viewHolder, T data, int position, int viewType) {
}
});
mAdapter.setOnItemChildClickListener(R.id.child, new OnItemChildClickListener<String>() {
@Override
public void onItemChildClick(ViewHolder viewHolder, String data, int position) {
}
});
// 可添加多个
mAdapter.addHeaderView(view)
mAdapter.isShowHeaderView()
mAdapter.openAutoLoadMore()
mAdapter.clearLoadView()
mAdapter.loadFailed();
mAdapter.loadEnd();
mAdapter.reset();
// 新data插入到原data的尾部(为加载更多封装的方法)
mAdapter.setLoadMoreData(data);
// 新data直接替换原data(为加载更多封装的方法)
mAdapter.setNewData(data);
// 通用插入数据
mAdapter.insert()
// 通用删除数据
mAdapter.remove()
// 通用更新数据
mAdapter.change()