I'm trying to use ViewBinding in the new module I added to the project that will contain all the CustomViews in the project.
This module at this moment is the only one that will implement the viewbinding. The main module (app), is using at this moment kotlin synthetic.
Originally I have this 2 custom views:
(Pre - ViewBinding --> This is working. I can see it in the fragment attached)
CustomView 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)
}
}
CustomView2:
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)
}
}
}
}
Then I added the viewBinding = true
in gradle file and delete the kotlin-android-extensions
plugin.
So the Customs looks like:
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)
}
}
}
}
Since here, all works fine. But when I open the fragment, the custom views are not displayed.
So in resume:
With Kotlkin synthetic is working and the views are displayed, with ViewBinding are not workin.
Whats is wrong?
Try replacing this line:
private val binding = LayoutHorizontalFilterBinding.inflate(LayoutInflater.from(context))
with this:
private val binding = LayoutHorizontalFilterBinding.inflate(LayoutInflater.from(context), this, true)
This doesn't work anymore since the latest version. Inflate only accepts two arguments and attachToParent, the last one is gone.