- 1.支持header和footer
- 2.支持loader和 no more layout
- 3.支持带泛型的单类型,多类型布局
- 4.支持不带泛型的多类型模块用法
- 5.支持不同delegate不同span size
compile 'com.dengzq.widget:baservadapter:0.8.6'
val header = layoutInflater.inflate(R.layout.layout_header, recycler_view, false)
adapter.addHeaderView(header)
如果需要在某些情况下移除header,请在添加时绑定一个key
add : adapter.addHeaderView(key,header)
remove: adapter.removeHeader(key)
val footer = layoutInflater.inflate(R.layout.layout_footer, recycler_view, false)
adapter.addFooterView(footer)
//移除footer
adapter.addFooterView(key, footer)
adapter.removeFooter(key)
class LoaderView : ILoaderView {
override fun getLoadingLayoutId(): Int = R.layout.layout_loader
override fun getErrorLayoutId(): Int = R.layout.layout_load_err
}
val loader = LoaderView()
adapter.addLoaderView(loader)
//默认情况下,添加loader则开启加载更多功能,如果需要关闭:
//adapter.removeLoaderView() or adapter.openLoadMore(false)
adapter.loadMoreListener = object : OnLoadMoreListener {
override fun onLoadMore() {
//do what you want to add data here;
}
}
当loadMore请求结束,请回调
adapter.loadMoreSuccess() //loadMore success
adapter.loadMoreFail() //loadMore fail, show load error layout if it's exist;
1)默认情况下,加载更多使用自动加载
方式,如果需要点击加载
的方式,请通过loader的点击事件实现;
2)加载失败调用adapter.loadMorefail()
会展示加载失败视图,可以通过loader点击事件实现重新请求;
特别的,adapter.goReloading()
会重新回调onLoadMoreListener
. 如果你的请求逻辑没有写在onLoadMoreListener
内,
请自行在 state==LoadState.ERROR
时实现重新请求逻辑.
adapter.autoLoadMore(false)
adapter.loaderClickListener=object :OnLoaderClickListener{
override fun onItemClick(v: View, holder: BaseViewHolder, state: LoadState) {
if (state==LoadState.LOADING){
//Click to loadMore data here;
}
else if (state==LoadState.ERROR){
//error layout was clicked, reload data;
adapter.goReloading()
}
}
}
1.添加BottomView
adapter.addBottomView(view)
2.无更多时,请调用
adapter.isHasMore(false)
3.刷新数据时,请重置isHasMore状态
adapter.isHasMore(true)
class SingleTypeAdapter(context: Context, list: ArrayList<T>) : CommonAdapter<T>(context, layout, list) {
override fun convert(holder: BaseViewHolder, t: T, position: Int) {
//do what you want when bindViewHolder
}
}
1).继承ItemViewDelegate<T>,实现当前类型item的视图功能
class MultiModelDelegate:ItemViewDelegate<ModelBean>(){
override fun isForViewType(t: ModelBean, position: Int): Boolean=t.type==0
override fun getItemViewLayoutId(): Int= R.layout.item_adapter_common
override fun convert(holder: BaseViewHolder, t: ModelBean, position: Int) {
holder.setText(R.id.tv_nick, t.nick)
}
}
2).继承MultiItemTypeAdapter<T>,将delegate添加到adapter
class MultiItemAdapter(context: Context, list: List<ModelBean>) : MultiItemTypeAdapter<ModelBean>(context, list) {
init {
addItemViewDelegate(MultiModelDelegate())
addItemViewDelegate(MultiTextDelegate())
}
}
3).设置adapter
recycler_view.adapter = MultiItemAdapter(context, list)
1).继承ItemClassifyDelegate,实现当前类型item的视图功能
class ClassifyMsgDelegate(private val presenter: IClassifyPresenter) : ItemClassifyDelegate() {
override fun getItemSize(): Int = presenter.getClassifyNews().size
override fun needShow(): Boolean = getItemSize() > 0
override fun getItemViewLayoutId(): Int = R.layout.item_adapter_text
override fun convert(holder: BaseViewHolder, position: Int) {
}
}
2).继承MultiItemClassifyAdapter,添加delegate
class ClassifyAdapter(context: Context, presenter: IClassifyPresenter) : MultiItemClassifyAdapter(context) {
init {
addItemClassifyDelegate(ClassifyBannerDelegate(context, presenter))
addItemClassifyDelegate(ClassifyMenuDelegate(presenter))
}
}
3).设置adapter
recycler_view.adapter = ClassifyAdapter(activity!!, presenter)
//重写getItemSpanSize(position: Int)方法;
class GridSpanDelegate : ItemViewDelegate<ModelBean>() {
override fun getItemSpanSize(position: Int): Int {
return when (position % 7) {
0 -> 4
1, 2 -> 2
else -> 1
}
}
}
1.预加载布局
2.拖拽、侧滑、分级
3.动画
etc.
请结合SimpleRefreshLayout实现或查看demo;