fix: fragment基类问题

This commit is contained in:
Quyunshuo
2020-09-26 18:05:06 +08:00
parent bc9772a828
commit 0dcc3bf105
3 changed files with 13 additions and 11 deletions

View File

@ -22,8 +22,12 @@ import java.lang.reflect.ParameterizedType
abstract class BaseFrameFragment<VB : ViewBinding, VM : ViewModel> :
Fragment() {
protected lateinit var mBinding: VB
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) {
val vbClass: Class<VB> =
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>
val inflate = vbClass.getMethod("inflate", LayoutInflater::class.java)
inflate.invoke(null, layoutInflater) as VB
}
protected val mViewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) {
val vmClass: Class<VM> =
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VM>
@ -38,10 +42,6 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : ViewModel> :
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val vbClass: Class<VB> =
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>
val inflate = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
mBinding = inflate.invoke(null, layoutInflater, container, false) as VB
return mBinding.root
}

View File

@ -23,6 +23,7 @@ abstract class BaseFrameNotMVVMActivity<VB : ViewBinding> : AppCompatActivity()
val inflate = vbClass.getMethod("inflate", LayoutInflater::class.java)
inflate.invoke(null, layoutInflater) as VB
}
protected abstract fun initView()
override fun onCreate(savedInstanceState: Bundle?) {

View File

@ -19,7 +19,12 @@ import java.lang.reflect.ParameterizedType
*/
abstract class BaseFrameNotMVVMFragment<VB : ViewBinding> : Fragment() {
protected lateinit var mBinding: VB
protected val mBinding: VB by lazy(mode = LazyThreadSafetyMode.NONE) {
val vbClass: Class<VB> =
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>
val inflate = vbClass.getMethod("inflate", LayoutInflater::class.java)
inflate.invoke(null, layoutInflater) as VB
}
protected abstract fun initView()
@ -28,10 +33,6 @@ abstract class BaseFrameNotMVVMFragment<VB : ViewBinding> : Fragment() {
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val vbClass: Class<VB> =
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VB>
val inflate = vbClass.getDeclaredMethod("inflate", LayoutInflater::class.java)
mBinding = inflate.invoke(null, layoutInflater, container, false) as VB
return mBinding.root
}