android viewbinding 自动填充工具

`
import android.content.Context
import android.view.LayoutInflater
import androidx.lifecycle.*
import androidx.viewbinding.ViewBinding
import java.lang.reflect.ParameterizedType

/**

  • 查找某个类找到带有指定泛型的具体类
    /
    fun getGenericSuperclass(clazz: Class<
    >, typClass: Class<>): Class<>? {
    val superclass = clazz.genericSuperclass
    if (superclass is ParameterizedType) {
    val actualTypeArguments = superclass.actualTypeArguments
    val d = actualTypeArguments.firstOrNull {
    it is Class<*> && typClass.isAssignableFrom(it)
    }
    if (d != null) {
    return clazz
    }
    }
    return getGenericSuperclass(clazz.superclass, typClass);
    }

//------------------------------------ViewBinding-----------------------------------

/**

  • 根据泛型 BINDING 创建 ViewDataBinding 实例
    /
    inline fun < reified BINDING> createViewBinding(javaClass: Class<
    >,context: Context): BINDING {
    return getViewBindingType(javaClass) // 获取 ViewDataBinding 泛型实际类型
    ?.getMethod("inflate", LayoutInflater::class.java) // 反射获取 inflate 方法
    ?.invoke(null, LayoutInflater.from(context)) as BINDING // 通过反射调用 inflate 方法
    }
    inline fun createViewBinding(any:Any,context: Context): BINDING {
    return getViewBindingType(any::class.java) // 获取 ViewDataBinding 泛型实际类型
    ?.getMethod("inflate", LayoutInflater::class.java) // 反射获取 inflate 方法
    ?.invoke(null, LayoutInflater.from(context)) as BINDING // 通过反射调用 inflate 方法
    }

fun getViewBindingType(clazz: Class<>): Class<>? {
// val superclass = clazz.genericSuperclass
val superclass = getGenericSuperclass(clazz, ViewBinding::class.java)?.genericSuperclass
if (superclass is ParameterizedType) {
//返回表示此类型实际类型参数的 Type 对象的数组
val actualTypeArguments = superclass.actualTypeArguments
return actualTypeArguments.firstOrNull {
// 判断是 Class 类型 且是 ViewDataBinding 的子类
it is Class<> && ViewBinding::class.java.isAssignableFrom(it)
} as? Class<
>
}
return null
}

//--------------------------------viewDataBinding-------------------------------------------------------

/**

  • 根据泛型 BINDING 创建 ViewDataBinding 实例
    /
    //inline fun createViewDataBinding(context: Context): BINDING {
    // return getViewDataBindingType(BINDING::class.java) // 获取 ViewDataBinding 泛型实际类型
    // ?.getMethod("inflate", LayoutInflater::class.java) // 反射获取 inflate 方法
    // ?.invoke(null, LayoutInflater.from(context)) as BINDING // 通过反射调用 inflate 方法
    //}
    //
    //fun getViewDataBindingType(clazz: Class<
    >): Class<>? {
    //// val superclass = clazz.genericSuperclass
    // val superclass = getGenericSuperclass(clazz, ViewDataBinding::class.java)?.genericSuperclass
    // if (superclass is ParameterizedType) {
    // //返回表示此类型实际类型参数的 Type 对象的数组
    // val actualTypeArguments = superclass.actualTypeArguments
    // return actualTypeArguments.firstOrNull {
    // // 判断是 Class 类型 且是 ViewDataBinding 的子类
    // it is Class<
    > && ViewDataBinding::class.java.isAssignableFrom(it)
    // } as? Class<*>
    // }
    // return null
    //}

//--------------------------------viewModle-------------------------------------------------------
/**
*

  • @Description:
  • @Author: loongwind
  • @CreateDate:2020/8/13 6:07 AM

/
fun getViewModelType(clazz: Class<
>): Class? {
val superclass = clazz.genericSuperclass
if (superclass is ParameterizedType) {
//返回表示此类型实际类型参数的 Type 对象的数组
val actualTypeArguments = superclass.actualTypeArguments
//返回第一个符合条件的 Type 对象
return actualTypeArguments.firstOrNull {
it is Class<*> && ViewModel::class.java.isAssignableFrom(it)
} as? Class
}
return null
}

/**

  • 简化ViewModelProvider 使用
    */
    //inline fun ComponentActivity.viewModelProvider(vararg parameters: Any?): Lazy {
    // return lazy(LazyThreadSafetyMode.NONE) {
    // val factory = CommonViewModeFactory(T::class.java, *parameters)
    // ViewModelProvider(this, factory)[T::class.java]
    // }
    //}
    //

/**

  • 简化ViewModelProvider 使用
    */
    //inline fun Fragment.viewModelProvider(vararg parameters: Any?): Lazy {
    // return lazy(LazyThreadSafetyMode.NONE) {
    // val factory = CommonViewModeFactory(T::class.java, *parameters)
    // ViewModelProvider(this, factory)[T::class.java]
    // }
    //}

//
//@OptIn(KoinInternalApi::class)
//fun ComponentActivity.injectViewModel() : ViewModel?{
// return getViewModel(javaClass, getKoinScope(), this, viewModelStore )
//}
//
//@OptIn(KoinInternalApi::class)
//fun Fragment.injectViewModel() : ViewModel?{
// return getViewModel(javaClass, getKoinScope(), this, viewModelStore )
//}
//
///**
// * @param javaClass Class类型
// * @param scope koin生命周期范围
// * @param owner ViewModelStoreOwner 类型,ViewModel 绑定什么周期对象,Activity、Fragment 都实现了该接口
// * @param viewModelStore 存储 ViewModel 的对象
// /
//@OptIn(KoinInternalApi::class)
//fun getViewModel(javaClass : Class<
>,
// scope: Scope,
// owner: ViewModelStoreOwner,
// viewModelStore: ViewModelStore) : ViewModel?{
// // 获取当前 Activity 上 ViewModel 泛型的实际类型
// val viewModel = getViewModelType(javaClass)?.let {
// // 获取 ViewModelFactory
// val viewModelFactory = getViewModelFactory(owner, it.kotlin, null, null, null, scope)
// //获取注入的 ViewModel
// ViewModelLazy(it.kotlin, { viewModelStore }, { viewModelFactory} ).value
// }
// return viewModel
//}
//
//
`
用法
abstract class BaseFragment : Fragment() {
protected lateinit var binding: T
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
binding = createViewBinding(inflater, container)
}

posted @ 2024-06-05 11:24  烟花易冷心易碎  阅读(15)  评论(0编辑  收藏  举报