gpt4 book ai didi

android - 如何使用 View 绑定(bind)制作 BaseFragment

转载 作者:行者123 更新时间:2023-12-03 13:59:04 24 4
gpt4 key购买 nike

我的 基 fragment :

abstract class BaseFragment<ViewModel : BaseViewModel, Binding : ViewBinding> : Fragment() {

protected abstract val viewModel: ViewModel
private var _binding: Binding? = null
protected val binding get() = _binding!!

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
_binding = Binding.inflate(inflater, container, false) //This line not working
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

initialize()
setupListeners()
observe()
}

abstract fun initialize()

abstract fun setupListeners()

abstract fun observe()

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}
但是这条线不起作用 _binding = Binding.inflate(inflater, container, false)有一个工作代码,但对我来说这是狗屎编码:
abstract class BaseFragment<ViewModel : BaseViewModel, T : ViewBinding>() : Fragment() {

private var _binding:T? = null
protected abstract val viewModel: ViewModel
protected val binding get() = _binding!!


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View? {
val superclass: Type = javaClass.genericSuperclass!!
val aClass = (superclass as ParameterizedType).actualTypeArguments[1] as Class<*>
try {
val method: Method = aClass.getDeclaredMethod(
"inflate",
LayoutInflater::class.java,
ViewGroup::class.java,
Boolean::class.javaPrimitiveType
)
_binding = method.invoke(null, layoutInflater, container, false) as T

} catch (e: Exception) {
e.printStackTrace()
}
return binding.root
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
initialize()
setupListeners()
observe()
}

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}

abstract fun initialize()

abstract fun setupListeners()

abstract fun observe()
}
如何正确地做任何人都有一个干净的解决方案。我知道 数据绑定(bind) 有 DataBindingUtil 但我需要 View 绑定(bind) . 有类似的东西吗? View 绑定(bind)
我希望你的答案
.
编辑
图书馆: https://github.com/kirich1409/ViewBindingPropertyDelegate
基本 fragment :
abstract class BaseFragment<ViewModel : BaseViewModel, Binding : ViewBinding>(
layoutID: Int
) : Fragment(layoutID) {

protected abstract val viewModel: ViewModel
protected abstract val binding: Binding

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

setupViews()
setupListeners()
setupObservers()
}

abstract fun setupViews()

abstract fun setupListeners()

abstract fun setupObservers()
}
class Fragment : BaseFragment<BaseViewModel, FragmentBinding>(
R.layout.fragment
) {

override val viewModel: BaseViewModel by viewModels()
override val binding: FragmentBinding by viewBinding() // this is from library

override fun setupViews() {

}

override fun setupListeners() {

}

override fun setupObservers() {

}
}

最佳答案

BaseFragment.kt

typealias Inflate<T> = (LayoutInflater, ViewGroup?, Boolean) -> T

abstract class BaseFragment<VB: ViewBinding>(
private val inflate: Inflate<VB>
) : Fragment() {

private var _binding: VB? = null
val binding get() = _binding!!

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

override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
HomeFragment.kt
class HomeFragment() : BaseFragment<FragmentHomeBinding>(FragmentHomeBinding::inflate) {

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

binding.homeText.text = "Hello view binding"
}
}

关于android - 如何使用 View 绑定(bind)制作 BaseFragment,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64819181/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com