代码重构之美
随着代码量增多,越来越觉得有必要构建fragment基类,今天实在受不了,于是重构开始…
注入viewmodel
实际上是要真正意义上对viewmodel进行自动注入的,由于我使用的是viewmodelFactory需要传参所以这里没有实现注入
BaseVmFragment.kt
package com.example.module_main.base
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModel
abstract class BaseVmFragment<VM: ViewModel>: Fragment() {
lateinit var mActivity: AppCompatActivity
abstract var isHandleFragmentAgainOnCreateView: Boolean
protected var isNavigationViewInit = false
protected var lastView: View? = null
override fun onAttach(context: Context) {
super.onAttach(context)
mActivity = activity as AppCompatActivity
onFragmentAttach()
}
abstract fun onFragmentAttach()
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? = inflater.inflate(layoutId(),container,false)
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
if(isHandleFragmentAgainOnCreateView){
if(!isNavigationViewInit){
init(savedInstanceState)
isNavigationViewInit = true
}
}else{
init(savedInstanceState)
}
}
fun init(savedInstanceState: Bundle?){
initBeforeBinding(savedInstanceState)
initBinding(savedInstanceState)
initAfterBinding(savedInstanceState)
}
abstract fun initAfterBinding(savedInstanceState: Bundle?)
abstract fun initBeforeBinding(savedInstanceState: Bundle?)
abstract fun initBinding(savedInstanceState: Bundle?)
abstract fun layoutId(): Int
}
注入DataBinding
BaseVmDbFragment.kt
package com.example.module_main.base
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
abstract class BaseVmDbFragment<VM : ViewModel, DB : ViewDataBinding> : BaseVmFragment<VM>() {
private var _binding: DB? = null
val mBinding: DB get() = _binding!!
abstract override var isHandleFragmentAgainOnCreateView: Boolean
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
if (isHandleFragmentAgainOnCreateView) {
if (lastView == null) {
_binding = DataBindingUtil.inflate(inflater, layoutId(), container, false)
lastView = mBinding.root
}
return lastView
} else {
_binding = DataBindingUtil.inflate(inflater, layoutId(), container, false)
mBinding.lifecycleOwner = this
lastView = mBinding.root
return lastView
}
}
override fun onDestroy() {
super.onDestroy()
onFragmentDestroy()
_binding = null
}
abstract fun onFragmentDestroy()
abstract override fun onFragmentAttach()
}
fragment基类的创建
BaseFragment.kt
package com.example.module_main.base
import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.ViewModel
abstract class BaseFragment<VM: ViewModel, DB: ViewDataBinding>: BaseVmDbFragment<VM, DB>(){
abstract override var isHandleFragmentAgainOnCreateView: Boolean
abstract override fun onFragmentDestroy()
abstract override fun onFragmentAttach()
abstract override fun layoutId(): Int
abstract override fun initBeforeBinding(savedInstanceState: Bundle?)
abstract override fun initBinding(savedInstanceState: Bundle?)
abstract override fun initAfterBinding(savedInstanceState: Bundle?)
}
|