interface MainContract {
interface View : IView {
fun showBanners(banners: MutableList<Banner>)
}
interface Presenter : IPresenter<View> {
fun getBanner2()
}
interface Model : IModel {
fun getBanners(): Observable<HttpResult<MutableList<Banner>>>
}
}
第三步:创建一个Model接口的实现类,需要继承 BaseModel 类 并实现Model接口
案例: MainModel
class MainModel : BaseModel(), MainContract.Model {
override fun getBanners(): Observable<HttpResult<MutableList<Banner>>> {
return MainRetrofit.service.getHomeBanner()
}
}
第四步:创建一个Presenter接口的实现类,需要继承 BasePresenter<M : IModel, V : IView> 并实现 MainContract.Presenter 接口
案例: MainPresenter
class MainPresenter : BasePresenter<MainContract.Model, MainContract.View>(), MainContract.Presenter {
override fun createModel(): MainContract.Model? = MainModel()
override fun getBanner2() {
mModel?.getBanners()?.ss(mModel, mView) {
mView?.showBanners(it.data)
}
}
}
第五步:创建一个View的接口实现类,这是一个Activity或者Fragment,需要继承BaseMvpActivity<in V : IView, P : IPresenter> 或者 BaseMvpFragment<in V : IView, P : IPresenter> 并实现 View 接口
案例:MainActivity
class MainActivity : BaseMvpTitleActivity<MainContract.View, MainContract.Presenter>(), MainContract.View {
override fun attachChildLayoutRes(): Int = R.layout.activity_main
override fun createPresenter(): MainContract.Presenter = MainPresenter()
override fun initView() {
}
override fun initData() {
}
override fun start() {
mPresenter?.getBanner2()
}
override fun showBanners(banners: MutableList<Banner>) {
tv_result.text = banners.toString()
}
}
Copyright 2018 iceCola7
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
请发表评论