当前位置:   article > 正文

Android ViewBinding 封装使用_viewbinding封装

viewbinding封装

1、ViewBinding + Activity 封装

2、ViewBinding + Fragment 封装

3、ViewBinding + DaiglogFragment 封装

4、ViewBinding + Adapter 封装

1、ViewBinding + Activity 封装

  1. open abstract class BaseMVVMActivity<VB : ViewBinding, VM : BaseVM> : FragmentActivity() {
  2. var mContext: Context? = null
  3. var mActivity: Activity? = null
  4. var mBinding: VB? = null
  5. var mVM: VM? = null
  6. override fun onCreate(savedInstanceState: Bundle?) {
  7. super.onCreate(savedInstanceState)
  8. mContext = this
  9. mActivity = this
  10. if (isFullWindow()){
  11. ImmersionBar.with(this)
  12. .statusBarDarkFont(statusBarDarkFont())
  13. .init();
  14. }
  15. //拿到泛型
  16. val superClass: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
  17. //初始化binding
  18. val classVB = superClass.actualTypeArguments[0] as Class<VB>
  19. val vbMethod = classVB.getMethod("inflate", LayoutInflater::class.java)
  20. mBinding = vbMethod.invoke(null, layoutInflater) as VB
  21. //初始化VM
  22. val classVM = superClass.actualTypeArguments[1] as Class<VM>
  23. mVM = ViewModelProvider(this).get(classVM)
  24. setContentView(mBinding!!.root)
  25. initView()
  26. initObserve()
  27. initData()
  28. }
  29. //是否全屏
  30. open fun isFullWindow():Boolean = true
  31. //状态栏字体是深色
  32. open fun statusBarDarkFont():Boolean = true
  33. abstract fun initView()
  34. abstract fun initObserve()
  35. open abstract fun initData()
  36. override fun onDestroy() {
  37. super.onDestroy()
  38. mBinding = null
  39. }
  40. }

使用

  1. class MainActivity : BaseMVVMActivity<ActivityMainBinding, MainActivityVM>(){
  2. override fun initView() {
  3. mBinding?.btnNext?.setOnClickListener {
  4. val intent = Intent(mActivity,Test2Activity::class.java)
  5. startActivity(intent)
  6. }
  7. }
  8. override fun initObserve() {
  9. }
  10. override fun initData() {
  11. }
  12. }

2、ViewBinding + Fragment 封装

  1. open abstract class BaseMVVMFragment<VB : ViewBinding, VM : BaseVM>:Fragment(){
  2. var mBinding: VB? = null
  3. var mVM: VM? = null
  4. override fun onCreateView(
  5. inflater: LayoutInflater,
  6. container: ViewGroup?,
  7. savedInstanceState: Bundle?
  8. ): View? {
  9. //拿到泛型
  10. val superClass: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
  11. //初始化binding
  12. val classVB = superClass.actualTypeArguments[0] as Class<VB>
  13. val vbMethod = classVB.getMethod("inflate", LayoutInflater::class.java,ViewGroup::class.java,Boolean::class.java)
  14. mBinding = vbMethod.invoke(null, inflater,container,false) as VB
  15. //初始化VM
  16. val classVM = superClass.actualTypeArguments[1] as Class<VM>
  17. mVM = ViewModelProvider(this).get(classVM)
  18. initView()
  19. initObserve()
  20. initData()
  21. return mBinding!!.root
  22. }
  23. abstract fun initView()
  24. abstract fun initObserve()
  25. open abstract fun initData()
  26. override fun onDestroyView() {
  27. super.onDestroyView()
  28. mBinding = null
  29. }
  30. }

使用

  1. class TestFragment:BaseMVVMFragment<MainFragmentTestBinding,TestFragmentVM>(){
  2. override fun initView() {
  3. }
  4. override fun initObserve() {
  5. }
  6. override fun initData() {
  7. }
  8. }

3、ViewBinding + DaiglogFragment 封装

  1. open abstract class BaseDialogFragment<VB : ViewBinding, VM : BaseVM> : DialogFragment() {
  2. var mBinding: VB? = null
  3. var mVM: VM? = null
  4. override fun onCreate(savedInstanceState: Bundle?) {
  5. super.onCreate(savedInstanceState)
  6. setStyle(STYLE_NO_TITLE,R.style.MyDialog)
  7. isCancelable = true
  8. }
  9. override fun onCreateView(
  10. inflater: LayoutInflater,
  11. container: ViewGroup?,
  12. savedInstanceState: Bundle?
  13. ): View? {
  14. //拿到泛型
  15. val superClass: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
  16. //初始化binding
  17. val classVB = superClass.actualTypeArguments[0] as Class<VB>
  18. val vbMethod = classVB.getMethod(
  19. "inflate",
  20. LayoutInflater::class.java,
  21. ViewGroup::class.java,
  22. Boolean::class.java
  23. )
  24. mBinding = vbMethod.invoke(null, inflater, container, false) as VB
  25. //初始化VM
  26. val classVM = superClass.actualTypeArguments[1] as Class<VM>
  27. mVM = ViewModelProvider(this).get(classVM)
  28. initView()
  29. initObserve()
  30. initData()
  31. return mBinding!!.root
  32. }
  33. open abstract fun initView()
  34. open abstract fun initObserve()
  35. open abstract fun initData()
  36. }

MyDialog.style

  1. <style name="MyDialog" parent="Theme.AppCompat.Light.Dialog">
  2. <!-- <item name="bottomSheetStyle">@style/Theme.Design.BottomSheetDialog</item>-->
  3. <item name="android:background">@android:color/transparent</item>
  4. <item name="android:windowBackground">@android:color/transparent</item>
  5. <item name="android:windowNoTitle">true</item>
  6. <item name="android:windowIsFloating">true</item>
  7. <item name="android:windowIsTranslucent">false</item>
  8. </style>

使用

  1. class TestDialog: BaseDialogFragment<MainDialogTestBinding, TestDailogVM>(){
  2. override fun initView() {
  3. }
  4. override fun initObserve() {
  5. }
  6. override fun initData() {
  7. }
  8. }

---------------------------------- BottomSheetDialogFragment-------------------------------------

  1. open abstract class BaseBottomDialogFragment<VB : ViewBinding, VM : BaseVM>: BottomSheetDialogFragment(){
  2. var mBinding: VB? = null
  3. var mVM: VM? = null
  4. override fun onCreateView(
  5. inflater: LayoutInflater,
  6. container: ViewGroup?,
  7. savedInstanceState: Bundle?
  8. ): View? {
  9. //拿到泛型
  10. val superClass: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
  11. //初始化binding
  12. val classVB = superClass.actualTypeArguments[0] as Class<VB>
  13. val vbMethod = classVB.getMethod("inflate", LayoutInflater::class.java,ViewGroup::class.java,Boolean::class.java)
  14. mBinding = vbMethod.invoke(null, inflater,container,false) as VB
  15. //初始化VM
  16. val classVM = superClass.actualTypeArguments[1] as Class<VM>
  17. mVM = ViewModelProvider(this).get(classVM)
  18. initView()
  19. initObserve()
  20. initData()
  21. return mBinding!!.root
  22. }
  23. //禁用CollapsingToolbarLayout的展开/折叠.
  24. // mBinding.recyclerView.isNestedScrollingEnabled = false
  25. open abstract fun initView()
  26. open abstract fun initObserve()
  27. open abstract fun initData()
  28. }

4、ViewBinding + Adapter 封装

VBViewHolder
  1. class VBViewHolder<VB:ViewBinding>(val mBinding: VB) :BaseViewHolder(mBinding.root)
BaseVBAdapter
  1. abstract class BaseVBAdapter<T : Any, VB : ViewBinding>(mData: MutableList<T>? = null) :
  2. BaseQuickAdapter<T, VBViewHolder<VB>>(0, mData) {
  3. override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): VBViewHolder<VB> {
  4. //拿到泛型
  5. val superClass: ParameterizedType = javaClass.genericSuperclass as ParameterizedType
  6. //初始化binding
  7. val classVB = superClass.actualTypeArguments[1] as Class<VB>
  8. val vbMethod = classVB.getMethod(
  9. "inflate",
  10. LayoutInflater::class.java,
  11. ViewGroup::class.java,
  12. Boolean::class.java
  13. )
  14. val mBinding: VB =
  15. vbMethod.invoke(null, LayoutInflater.from(parent.context), parent, false) as VB
  16. return VBViewHolder(mBinding)
  17. }
  18. }

使用 

  1. class StudentAdapter : BaseVBAdapter<String, MainItemStudentBinding>() {
  2. override fun convert(holder: VBViewHolder<MainItemStudentBinding>, item: String) {
  3. holder?.mBinding?.tvName?.text = "${item}"
  4. }
  5. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小舞很执着/article/detail/1002321
推荐阅读
  

闽ICP备14008679号