fix: fragment基类问题
This commit is contained in:
@ -22,8 +22,12 @@ import java.lang.reflect.ParameterizedType
|
|||||||
abstract class BaseFrameFragment<VB : ViewBinding, VM : ViewModel> :
|
abstract class BaseFrameFragment<VB : ViewBinding, VM : ViewModel> :
|
||||||
Fragment() {
|
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) {
|
protected val mViewModel: VM by lazy(mode = LazyThreadSafetyMode.NONE) {
|
||||||
val vmClass: Class<VM> =
|
val vmClass: Class<VM> =
|
||||||
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VM>
|
(javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[1] as Class<VM>
|
||||||
@ -38,10 +42,6 @@ abstract class BaseFrameFragment<VB : ViewBinding, VM : ViewModel> :
|
|||||||
container: ViewGroup?,
|
container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
): View? {
|
): 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
|
return mBinding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -23,6 +23,7 @@ abstract class BaseFrameNotMVVMActivity<VB : ViewBinding> : AppCompatActivity()
|
|||||||
val inflate = vbClass.getMethod("inflate", LayoutInflater::class.java)
|
val inflate = vbClass.getMethod("inflate", LayoutInflater::class.java)
|
||||||
inflate.invoke(null, layoutInflater) as VB
|
inflate.invoke(null, layoutInflater) as VB
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract fun initView()
|
protected abstract fun initView()
|
||||||
|
|
||||||
override fun onCreate(savedInstanceState: Bundle?) {
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
|||||||
@ -19,7 +19,12 @@ import java.lang.reflect.ParameterizedType
|
|||||||
*/
|
*/
|
||||||
abstract class BaseFrameNotMVVMFragment<VB : ViewBinding> : Fragment() {
|
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()
|
protected abstract fun initView()
|
||||||
|
|
||||||
@ -28,10 +33,6 @@ abstract class BaseFrameNotMVVMFragment<VB : ViewBinding> : Fragment() {
|
|||||||
container: ViewGroup?,
|
container: ViewGroup?,
|
||||||
savedInstanceState: Bundle?
|
savedInstanceState: Bundle?
|
||||||
): View? {
|
): 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
|
return mBinding.root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user