前言
Kotlin+Retrofit+RxJava+MVP封装(一)
Kotlin+Retrofit+RxJava+MVP封装(二)
对BaseListActiviy的封装
因为在实际开发的项目中,列表是经常用到的,所以我在这里将其封装了一下,介绍一下思路
1.在封装时,我们想到在基类里面应该囊括了BaseListPresenter,和与期对应的Bean和Model实现类;
2.绑定默认的布局,设定RecyclerView样式,上拉和下拉所要加载的数据;
3.集合BaseAdapter,能够直接引入Item布局,Item点击事件以及Item中控件的数据绑定,涉及到了BaseViewHolder;
4.继承BaseListActivity,重写其中必要的方法。
详细理解以下代码,所注意地方我会在代码中说明:
abstract class BaseListActivity<H,A:BaseBean,B:BaseListModelImpl<A>>: BaseActivity<BaseListView, BaseListPresenter<A,B>>(), BaseListView ,getModelImpl<A,B>{
var mAdapter: BaseListAdapter<H>?=null
var mList:ArrayList<H>?= ArrayList()
override fun bindLayout(): Int {
//绑定RecyclerView布局
return R.layout.activity_list
}
override fun initData() {
//初始化RecyclerView
val layoutManager = LinearLayoutManager(this)
layoutManager.orientation = LinearLayoutManager.VERTICAL
mRecyclerview.layoutManager=layoutManager
mRecyclerview.setRefreshProgressStyle(ProgressStyle.BallSpinFadeLoader)
mRecyclerview.setLoadingMoreProgressStyle(ProgressStyle.Pacman)
mRecyclerview.setArrowImageView(R.drawable.iconfont_downgrey)
}
override fun initListener() {
mRecyclerview.setLoadingListener(object : XRecyclerView.LoadingListener{//RecyclerView的上拉和下拉
override fun onLoadMore() {
mPresenter!!.getMoreData()
}
override fun onRefresh() {
mPresenter!!.getRefreshData()
}
})
if(mAdapter==null){
mAdapter= object :BaseListAdapter<H>(mList!!,getItemLayout()){//初始化adapter
override fun convert(helper: BaseViewHolder, item: H, position: Int, size: Int) {
fitDates(helper, item, position, size)//item数据绑定
}
}
mRecyclerview.adapter=mAdapter
mRecyclerview.refresh()
}else{
mAdapter!!.notifyDataSetChanged()
}
mAdapter!!.setOnRecyclerViewItemChildClickListener(object : BaseListAdapter.OnRecyclerViewItemChildClickListener {//item点击事件的接口回调
override fun onItemChildClick(adapter: BaseListAdapter<*>, view: View, position: Int) {
onItemClick(view, position)
}
})
}
override fun loadMore(data: ArrayList<*>) {//下拉加载需要加载的数据
mList!!.addAll(data as ArrayList<H>)
mAdapter!!.notifyDataSetChanged()
mRecyclerview.loadMoreComplete()
}
@Suppress("UNCHECKED_CAST")
override fun showRefresh(data: ArrayList<*>) {//上拉刷新需要写入的数据
mList!!.clear()
mList!!.addAll(data as ArrayList<H>)
mAdapter!!.notifyDataSetChanged()
mRecyclerview.refreshComplete()
}
override fun hasNoMoreData() {//没有更多数据
mRecyclerview.setNoMore(true)
mAdapter!!.notifyDataSetChanged()
}
override fun createPresenter(): BaseListPresenter<A,B>{
return BaseListPresenter(this,this)//返回BaseListPresenter,其中A是继承BaseBean的Bean文件,B是Model的实现类
}
abstract fun getItemLayout():Int//得到item布局
protected abstract fun fitDates(helper: BaseViewHolder, bean: H, position: Int, size: Int)//数据匹配
protected open fun onItemClick(v: View, position: Int) {
}//item点击事件
}
如上所示,封装好BaseListActivity过后,一些列表界面就比较好写了,例如:
实现以上界面,就下面几行代码就搞定了,是不是很方面简单。
class MoneyDetailActivity: BaseListActivity<MoneyDetailBean.PageResultsBean,MoneyDetailBean,MoneyDetailModelImpl>() {
var mo:MoneyDetailModelImpl?= MoneyDetailModelImpl()
override fun getMyModelImpl(): MoneyDetailModelImpl {
return mo!!
}
override fun getItemLayout(): Int {
return R.layout.item
}
override fun fitDates(helper: BaseViewHolder, bean: MoneyDetailBean.PageResultsBean, position: Int, size: Int) {
helper.setText(R.id.text,bean.title)
}
}
关于BaseListPresenter就不用再重写了,直接使用
class BaseListPresenter<A:BaseBean,B:BaseListModelImpl<A>>(private val listViews: BaseListView,getModelImpl: getModelImpl<A, B>): BasePresenter<BaseListView>() {
var listmodel: BaseListModel?=null
init {
listmodel= getModelImpl.getMyModelImpl()
}
//得到更多数据
fun getMoreData(){
listmodel!!.getMoreData()
}
//得到刷新数据
fun getRefreshData(){
listmodel!!.getRefreshDta()
listmodel!!.loadComplete(object : BaseListModel.LoadComplete{
override fun hasNoMoreData() {
listViews.hasNoMoreData()
}
override fun getDataRefresh(mDatas: ArrayList<*>) {
listViews.showRefresh(mDatas)
}
override fun getDataFailed(baseBean: BaseBean) {
}
override fun getDataMore(mDatas: ArrayList<*>) {
listViews.loadMore(mDatas)
}
})
}
}
关于BaseListModelImpl,因为service和Bean都不一样,所以重写请求成功和需要请求的接口即可,
open class BaseListModelImpl<S:BaseBean>: BaseModel<S>(), BaseListModel {
override fun ServiceParams(params: HashMap<String, String>): Observable<S> {
return Observable.never()
}
var PAGE:Int?=1
var loadListeners:BaseListModel.LoadComplete?=null
var isMore:Boolean?=true
override fun getMoreData() {
PAGE = PAGE!!.plus(1)
isMore=true
PostParams()
}
override fun getRefreshDta() {
PAGE=1
isMore=false
PostParams()
}
override fun showNetError() {
}
override fun showDataError() {
}
override fun loadComplete(loadedListener: BaseListModel.LoadComplete) {
if(this.loadListeners==null){
this.loadListeners=loadedListener
}
}
override fun FailedOperation(e: Throwable?) {
}
override fun SuccessOperation(o: BaseBean) {
}
override fun Params(): HashMap<String, String>? {
ClearHashMap()
mParams!!.put("page", PAGE.toString())
return mParams
}
}
关于BaseViewHolder和BaseAdapter我就不详细介绍了,可以在源代码中看到。
源码地址