赞
踩
1、ViewBinding + Activity 封装
2、ViewBinding + Fragment 封装
3、ViewBinding + DaiglogFragment 封装
4、ViewBinding + Adapter 封装
- open abstract class BaseMVVMActivity<VB : ViewBinding, VM : BaseVM> : FragmentActivity() {
-
- var mContext: Context? = null
- var mActivity: Activity? = null
-
- var mBinding: VB? = null
- var mVM: VM? = null
-
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- mContext = this
- mActivity = this
-
- if (isFullWindow()){
- ImmersionBar.with(this)
- .statusBarDarkFont(statusBarDarkFont())
- .init();
- }
- //拿到泛型
- val superClass: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
-
- //初始化binding
- val classVB = superClass.actualTypeArguments[0] as Class<VB>
- val vbMethod = classVB.getMethod("inflate", LayoutInflater::class.java)
- mBinding = vbMethod.invoke(null, layoutInflater) as VB
-
- //初始化VM
- val classVM = superClass.actualTypeArguments[1] as Class<VM>
- mVM = ViewModelProvider(this).get(classVM)
-
- setContentView(mBinding!!.root)
-
- initView()
- initObserve()
- initData()
- }
-
- //是否全屏
- open fun isFullWindow():Boolean = true
- //状态栏字体是深色
- open fun statusBarDarkFont():Boolean = true
-
- abstract fun initView()
- abstract fun initObserve()
- open abstract fun initData()
-
- override fun onDestroy() {
- super.onDestroy()
- mBinding = null
- }
- }
使用
- class MainActivity : BaseMVVMActivity<ActivityMainBinding, MainActivityVM>(){
-
- override fun initView() {
- mBinding?.btnNext?.setOnClickListener {
- val intent = Intent(mActivity,Test2Activity::class.java)
- startActivity(intent)
- }
- }
-
- override fun initObserve() {
- }
-
- override fun initData() {
-
- }
- }
-
- open abstract class BaseMVVMFragment<VB : ViewBinding, VM : BaseVM>:Fragment(){
-
- var mBinding: VB? = null
- var mVM: VM? = null
-
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
-
- //拿到泛型
- val superClass: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
-
- //初始化binding
- val classVB = superClass.actualTypeArguments[0] as Class<VB>
- val vbMethod = classVB.getMethod("inflate", LayoutInflater::class.java,ViewGroup::class.java,Boolean::class.java)
- mBinding = vbMethod.invoke(null, inflater,container,false) as VB
-
- //初始化VM
- val classVM = superClass.actualTypeArguments[1] as Class<VM>
- mVM = ViewModelProvider(this).get(classVM)
-
- initView()
- initObserve()
- initData()
-
- return mBinding!!.root
- }
-
-
- abstract fun initView()
- abstract fun initObserve()
- open abstract fun initData()
-
- override fun onDestroyView() {
- super.onDestroyView()
-
- mBinding = null
- }
- }
使用
- class TestFragment:BaseMVVMFragment<MainFragmentTestBinding,TestFragmentVM>(){
- override fun initView() {
-
- }
-
- override fun initObserve() {
-
- }
-
- override fun initData() {
-
- }
- }
- open abstract class BaseDialogFragment<VB : ViewBinding, VM : BaseVM> : DialogFragment() {
-
- var mBinding: VB? = null
- var mVM: VM? = null
-
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setStyle(STYLE_NO_TITLE,R.style.MyDialog)
- isCancelable = true
- }
-
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
- //拿到泛型
- val superClass: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
-
- //初始化binding
- val classVB = superClass.actualTypeArguments[0] as Class<VB>
- val vbMethod = classVB.getMethod(
- "inflate",
- LayoutInflater::class.java,
- ViewGroup::class.java,
- Boolean::class.java
- )
- mBinding = vbMethod.invoke(null, inflater, container, false) as VB
-
- //初始化VM
- val classVM = superClass.actualTypeArguments[1] as Class<VM>
- mVM = ViewModelProvider(this).get(classVM)
-
- initView()
- initObserve()
- initData()
- return mBinding!!.root
- }
-
-
- open abstract fun initView()
- open abstract fun initObserve()
- open abstract fun initData()
-
- }
MyDialog.style
- <style name="MyDialog" parent="Theme.AppCompat.Light.Dialog">
- <!-- <item name="bottomSheetStyle">@style/Theme.Design.BottomSheetDialog</item>-->
- <item name="android:background">@android:color/transparent</item>
- <item name="android:windowBackground">@android:color/transparent</item>
- <item name="android:windowNoTitle">true</item>
- <item name="android:windowIsFloating">true</item>
- <item name="android:windowIsTranslucent">false</item>
- </style>
使用
- class TestDialog: BaseDialogFragment<MainDialogTestBinding, TestDailogVM>(){
-
- override fun initView() {
- }
-
- override fun initObserve() {
-
- }
-
- override fun initData() {
-
- }
-
- }
---------------------------------- BottomSheetDialogFragment-------------------------------------
- open abstract class BaseBottomDialogFragment<VB : ViewBinding, VM : BaseVM>: BottomSheetDialogFragment(){
-
- var mBinding: VB? = null
- var mVM: VM? = null
-
- override fun onCreateView(
- inflater: LayoutInflater,
- container: ViewGroup?,
- savedInstanceState: Bundle?
- ): View? {
-
- //拿到泛型
- val superClass: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
-
- //初始化binding
- val classVB = superClass.actualTypeArguments[0] as Class<VB>
- val vbMethod = classVB.getMethod("inflate", LayoutInflater::class.java,ViewGroup::class.java,Boolean::class.java)
- mBinding = vbMethod.invoke(null, inflater,container,false) as VB
-
- //初始化VM
- val classVM = superClass.actualTypeArguments[1] as Class<VM>
- mVM = ViewModelProvider(this).get(classVM)
-
- initView()
- initObserve()
- initData()
- return mBinding!!.root
- }
-
- //禁用CollapsingToolbarLayout的展开/折叠.
-
- // mBinding.recyclerView.isNestedScrollingEnabled = false
-
- open abstract fun initView()
- open abstract fun initObserve()
- open abstract fun initData()
- }
VBViewHolder
-
- class VBViewHolder<VB:ViewBinding>(val mBinding: VB) :BaseViewHolder(mBinding.root)
BaseVBAdapter
- abstract class BaseVBAdapter<T : Any, VB : ViewBinding>(mData: MutableList<T>? = null) :
- BaseQuickAdapter<T, VBViewHolder<VB>>(0, mData) {
-
- override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VBViewHolder<VB> {
- //拿到泛型
- val superClass: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
- //初始化binding
- val classVB = superClass.actualTypeArguments[1] as Class<VB>
- val vbMethod = classVB.getMethod(
- "inflate",
- LayoutInflater::class.java,
- ViewGroup::class.java,
- Boolean::class.java
- )
- val mBinding: VB =
- vbMethod.invoke(null, LayoutInflater.from(parent.context), parent, false) as VB
- return VBViewHolder(mBinding)
- }
-
- }
使用
- class StudentAdapter : BaseVBAdapter<String, MainItemStudentBinding>() {
- override fun convert(holder: VBViewHolder<MainItemStudentBinding>, item: String) {
- holder?.mBinding?.tvName?.text = "${item}"
- }
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。