是否有每个使用View Binding的绑定类的父类? [英] Is there a parent class for every binding class using View Binding?

查看:133
本文介绍了是否有每个使用View Binding的绑定类的父类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ViewBinding,并且试图减少创建一个Fragment的代码,该Fragment是一个抽象类并包含以下代码:

I am using ViewBinding and I am trying to reduce the code creating a Fragment that is an abstract class and contains this code:

abstract class MyFragment<T> : Fragment() {
    
    private var binding: T? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = getBinding()
        return binding.root
    }
    
    abstract fun getBinding(): T
}

要使其正常工作,我需要使T扩展一个类,并且该类必须是所有绑定类的父类.

To make it work I need to make T extend a class and this class needs to be the parent of all the binding classes.

所有生成的绑定类都有一个公共父级?如果是这样,那是什么?

All the generated binding classes have a common parent? If that's the case what is it?

推荐答案

应为 ViewBinding .该代码段应适用于基本片段.

It should be ViewBinding. The code snippet Should work for base fragment.

abstract class BaseFragment<V: ViewBinding> : Fragment(){
private var binding: V? = null

override fun onCreateView(
    inflater: LayoutInflater,
    container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    binding = getBinding()
    return binding?.root
}

abstract fun getBinding(): V
}

这篇关于是否有每个使用View Binding的绑定类的父类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆