老码农,没什么激情撸码,随便封装一下base类,写来玩玩
直接上源码
一、创建BaseActivity
1、使用viewBinding来绑定视图
2、利用代理实现状态栏、菊花扩展
abstract class BaseActivity<VB : ViewBinding> : AppCompatActivity() {protected lateinit var viewBinding: VB//状态栏代理private var toolbarStyle: DispenseToolbarStyle by DispenseToolbarStyleDelegate()//菊花val loadingAgent: ILoading? by lazy { LoadingManager.get().initLoading() }override fun onCreate(savedInstanceState: Bundle?) {super.onCreate(savedInstanceState)viewBinding = ViewBindingFactory.createViewBinding(this, layoutInflater)toolbarStyle = getToolBarStyle()setContentView(viewBinding.root)registerLoadSir()initUI(savedInstanceState)}override fun onDestroy() {super.onDestroy()ToastUtils.cancelToast()}/*** 初始化UI*/protected abstract fun initUI(savedInstanceState: Bundle?)/*** 注册菊花*/protected open fun registerLoadSir() {}/*** 可重写此方法,修改状态栏样式*/open fun getToolBarStyle(): DispenseToolbarStyle =DispenseToolbar.get().defaultToolbarStyle() ?: DispenseToolbarStyle.WHITE
}
二、创建状态栏枚举DispenseToolbarStyle
enum class DispenseToolbarStyle {WHITE, BLACK, TRANSLATE, CUSTOM
}
三、创建状态栏属性代理类DispenseToolbarStyleDelegate
class DispenseToolbarStyleDelegate {operator fun getValue(thisRef: Any?, property: KProperty<*>): DispenseToolbarStyle {return DispenseToolbarStyle.WHITE}operator fun setValue(thisRef: Any?, property: KProperty<*>, value: DispenseToolbarStyle) {val activity = thisRef as Activitywhen (value) {DispenseToolbarStyle.TRANSLATE -> {BarUtils.setStatusBarFullTransparent(activity)BarUtils.setStatusBarColor(activity, Color.TRANSPARENT)}DispenseToolbarStyle.CUSTOM -> {DispenseToolbar.get().customToolbarStyle(activity)}DispenseToolbarStyle.BLACK -> {BarUtils.setStatusBarColor(activity, Color.BLACK, true)BarUtils.setLightMode(activity)}else -> {BarUtils.setStatusBarColor(activity, Color.WHITE, true)BarUtils.setDarkMode(activity)}}}
}
四、创建状态栏接口DispenseToolbarInterface
interface DispenseToolbarInterface {/*** 默认主题* @description 若为null,则默认是ChannelToolbarStyle.WHITE*/fun defaultToolbarStyle(): DispenseToolbarStyle?/*** 自定义主题* @description 若设置主题为自定义,则需要在此方法设置主题。* 若自定义多个主题,可以利用参数activity来判断*/fun customToolbarStyle(activity: Activity)}
五、实现interface,DispenseToolbar 利用静态代理,可以进一步把interface扩展(假设这写代码作为一个base库封装)
class DispenseToolbar : DispenseToolbarInterface {private var channelToolbarImpl: DispenseToolbarInterface? = nullcompanion object {@Volatileprivate var sInstance: DispenseToolbar? = nullfun get(): DispenseToolbar {if (sInstance == null) {synchronized(DispenseToolbar::class.java) {if (sInstance == null) {sInstance = DispenseToolbar()}}}return sInstance!!}}fun init(channelToolbarImpl: DispenseToolbarInterface) {this.channelToolbarImpl = channelToolbarImpl}override fun defaultToolbarStyle(): DispenseToolbarStyle? {return channelToolbarImpl?.defaultToolbarStyle()}override fun customToolbarStyle(activity: Activity) {channelToolbarImpl?.customToolbarStyle(activity)}}
六、代理实现DispenseToolbarImpl,若需要自定义不同的style可以,利用activity参数判断
class DispenseToolbarImpl : DispenseToolbarInterface {override fun defaultToolbarStyle(): DispenseToolbarStyle? {return DispenseToolbarStyle.CUSTOM}override fun customToolbarStyle(activity: Activity) {BarUtils.setStatusBarColor(activity,ContextCompat.getColor(activity, R.color.barTheme),true)BarUtils.setDarkMode(activity)}
}
七、初始化静态代理,可在application初始化
DispenseToolbar.get().init(DispenseToolbarImpl())
此时菊花与BaseActivity的封装已完成。
这里的属性代理或静态代理也可以减去一层,看自己喜好。