gpt4 book ai didi

android - ViewBinding 未在自定义 View 中显示 View

转载 作者:行者123 更新时间:2023-12-05 00:13:19 26 4
gpt4 key购买 nike

我正在尝试在我添加到项目中的新模块中使用 ViewBinding,该模块将包含项目中的所有自定义 View 。

目前,这个模块是唯一实现 View 绑定(bind)的模块。主模块(app),使用的是 kotlin 合成。

最初我有这两个自定义 View :

(Pre - ViewBinding --> 这是有效的。我可以在附加的 fragment 中看到它)

自定义 View 1:

import kotlinx.android.synthetic.main.layout_horizontal_filter.view.*

class HorizontalFiltersView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

private val adapter: HorizontalFilterAdapter
private val decoration: HorizontalFilterDecorator

init {
LayoutInflater.from(context).inflate(R.layout.layout_horizontal_filter, this, true)
this.adapter = HorizontalFilterAdapter(context)
this.decoration = HorizontalFilterDecorator(context, 16)

recycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
recycler.adapter = this.adapter
recycler.addItemDecoration(this.decoration)
recycler.setHasFixedSize(true)
}

fun setFilters(filtersList: MutableList<HorizontalFilter>) {
this.adapter.refresh(filtersList)
}

}

自定义 View 2:

import kotlinx.android.synthetic.main.item_horizontal_filter.view.*
class HorizontalFilterItem @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

private var onClickAction: () -> Unit = {}

init {
LayoutInflater.from(context).inflate(R.layout.item_horizontal_filter, this, true)
setOnClickListener { onClickAction.invoke() }
}

fun setData(image: Int, title: String, action: () -> Unit = {}) {
filter_title.text = title
loadImage(image)
setClickAction(action)
}

fun setClickAction(action: () -> Unit) {
this.onClickAction = action
}

private fun loadImage(image: Int?) {
image?.let { img ->
filter_image?.let { imView ->
Glide.with(this).load(img).fitCenter().into(imView)
}
}
}
}

然后我在 build.gradle 文件中添加了 viewBinding = true 并删除了 kotlin-android-extensions 插件。

所以海关看起来像:

class HorizontalFiltersView @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr) {

private val adapter: HorizontalFilterAdapter
private val decoration: HorizontalFilterDecorator
private val binding = LayoutHorizontalFilterBinding.inflate(LayoutInflater.from(context))

init {
addView(binding.root)
this.adapter = HorizontalFilterAdapter(context)
this.decoration = HorizontalFilterDecorator(context, 16)

binding.recycler.layoutManager = LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)
binding.recycler.adapter = this.adapter
binding.recycler.addItemDecoration(this.decoration)
binding.recycler.setHasFixedSize(true)
}

fun setFilters(filtersList: MutableList<HorizontalFilter>) {
this.adapter.refresh(filtersList)
}

}

class HorizontalFilterItem @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = 0
) : FrameLayout(context, attrs, defStyleAttr) {

private val binding = ItemHorizontalFilterBinding.inflate(LayoutInflater.from(context))
private var onClickAction: () -> Unit = {}

init {
addView(binding.root)
setOnClickListener { onClickAction.invoke() }
}

fun setData(image: Int, title: String, action: () -> Unit = {}) {
binding.filterTitle.text = title
loadImage(image)
setClickAction(action)
}

fun setClickAction(action: () -> Unit) {
this.onClickAction = action
}

private fun loadImage(image: Int?) {
image?.let { img ->
binding.filterImage.let { imView ->
Glide.with(this).load(img).fitCenter().into(imView)
}
}
}
}

到目前为止,一切正常。但是当我打开 fragment 时,不会显示自定义 View 。

总之:

使用 Kotlin 合成它可以工作并显示 View ;使用 ViewBinding 它不起作用。

怎么了?

最佳答案

尝试替换这一行:

    private val binding = LayoutHorizontalFilterBinding.inflate(LayoutInflater.from(context))

用这个:

    private val binding = LayoutHorizontalFilterBinding.inflate(LayoutInflater.from(context), this, true)

关于android - ViewBinding 未在自定义 View 中显示 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65140764/

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