我们在使用Activity的过程中,基本会重复一部分代码,今天我们写一个我们自己的统一父类,便于我们开发使用
注释:
这里继承于RxAppCompatActivity()和实现CustomAdapt用于适配
重写方法isBaseOnWidth()和getSizeInDp()
具体使用点击查看
一: BaseActivity
第一步:创建BaseActivity
abstract class BaseActivity : RxAppCompatActivity(), CustomAdapt {
lateinit var mActivity: Activity
override fun isBaseOnWidth(): Boolean {
return false
}
override fun getSizeInDp(): Float {
return 640F
}
abstract val layout: Int
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(layout)
mActivity = this
initWindow()
init()
request()
initIntent()
initPresenter()
initView()
initEvent()
getData()
initVisible()
}
private fun init() {
App.getInstance().registerActivity(this)
}
open fun initIntent() {}
private fun initWindow() { }
open fun initView() {}
open fun initEvent() {}
open fun getData() {}
open fun initPresenter() {}
open fun initVisible() {}
override fun onBackPressed() {}
override fun onDestroy() {
super.onDestroy()
App.getInstance().unregisterActivity(this)
}
fun initTitle(title: String, bl_back: Boolean, right: String) {
title_name.text = title
if (bl_back)
title_back.visibility = View.VISIBLE
if (right.isNotEmpty()) {
title_right.visibility = View.VISIBLE
title_right.text = right
}
title_back.setOnClickListener { finish() }
}
protected open fun showToast(s: String) {
ToastUtils.showShort(s)
}
}
第二步:创建MainActivity继承于BaseActivity
class MainActivity : BaseActivity() {
override val layout = R.layout.a_main
}
二: BaseFragment
abstract class BaseFragment : RxFragment() {
lateinit var mContext: Context
private var rootView: View? = null
private var isViewPrepared = false //标识fragment视图已经初始化完毕
private var hasFetchData = false //标识已经触发过懒加载数据
override fun onAttach(mContext: Context) {
super.onAttach(mContext)
this.mContext = mContext
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
rootView?.let {
(it.parent as ViewGroup?)?.removeView(it)
} ?: let {
initIntent(arguments)
rootView = inflater.inflate(layout, container, false)
}
return rootView
}
open fun initIntent(arguments: Bundle?) {}
abstract val layout: Int
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
initPresenter()
initView()
initEvent()
initType()
}
override fun onResume() {
super.onResume()
getData()
}
open fun getData() {}
open fun initType() {}
open fun initView() {}
open fun initEvent() {}
open fun initPresenter() {}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
isViewPrepared = true
lazyFetchDataIfPrepared()
}
private fun lazyFetchDataIfPrepared() {
//用户可见fragment && 没有加载过数据 && 视图已经准备完毕
if (!hasFetchData && isViewPrepared) {
hasFetchData = true
}
}
override fun onDestroyView() {
super.onDestroyView()
//view被销毁后,将可以重新触发数据懒加载,因为在viewpager下,fragment不会再次新建并走onCreate的生命周期流程,将从onCreateView开始
hasFetchData = false
isViewPrepared = false
}
// fun getProvider() = mContext as BaseActivity
}