Proper implementation with DataBinding
See original GitHub issueI’m using databinding library just as view holder. I have read through the wiki page, but I couldn’t find the way with using databinding just as view holder. (I don’t want to use ButterKnife and KotlinAndroidExtensions.) So I’m trying the code below, but I wonder if it is correct.
@EpoxyModelClass(layout = R.layout.item_hoge)
abstract class HogeItemModel : EpoxyModelWithHolder<DataBindingEpoxyHolder>() {
@EpoxyAttribute lateinit var hoge: Hoge
override fun bind(holder: DataBindingEpoxyHolder) {
val binding = holder.binding as? ItemHogeBinding ?: return
val context = binding.root.context
Picasso.with(context).load(hoge.imageUrl).fit().into(binding.image)
}
}
class DataBindingEpoxyHolder : EpoxyHolder() {
@CallSuper
override fun bindView(itemView: View) {
binding = DataBindingUtil.bind(itemView)
}
lateinit var binding: ViewDataBinding
}
Do you have any suggestions?
My original adapter code(without using Epoxy) is below.
open class HogeItemAdapter(var context: Context, var items: MutableList<HogeItem>)
: RecyclerView.Adapter<BindingHolder<ItemHogeBinding>>() {
override fun onCreateViewHolder(parent: ViewGroup,
viewType: Int): BindingHolder<ItemHogeBinding> {
return BindingHolder(context, parent,
R.layout.item_hoge)
}
override fun onBindViewHolder(holder: BindingHolder<ItemHogeBinding>,
position: Int) {
val binding = holder.binding
val item = items[holder.adapterPosition]
Picasso.with(context)
.load(item.imageUrl)
.fit()
.into(binding.image)
}
}
class BindingHolder<out T : ViewDataBinding>(context: Context,
parent: ViewGroup,
@LayoutRes layoutResId: Int)
: RecyclerView.ViewHolder(LayoutInflater.from(context).inflate(layoutResId, parent, false)) {
val binding: T = DataBindingUtil.bind(itemView)
}
Issue Analytics
- State:
- Created 6 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Using data binding in Android - Tutorial - Vogella.com
This tutorial describes the usage of data binding in Android applications. Data binding allows to synchronize your user interface with your application ...
Read more >Data Binding Library - Android Developers
The Data Binding Library provides classes and methods to easily observe data for changes. You don't have to worry about refreshing the UI...
Read more >Data Binding in Android: A tutorial with examples
Data binding is the process of integrating views in an XML layout with data objects. The Data Binding Library is responsible for generating...
Read more >Databinding in Android. Learn the easiest way to bind data to…
Step 2: Convert your layout files to Data Binding layouts · Firstly, wrap your layout containers with a tag <layout> . Just change...
Read more >Data Binding in Android with Example - GeeksforGeeks
Step by Step Implementation · Step 1: Create a New Project · Step 2. Enable DataBinding · Step 3. Working on XML files....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@elihart The code that you have suggested worked perfectly fine. 😄 I am sorry I didn’t explain it clearly enough. 💦
I’m using data binding library to avoid using findViewById.(just instead of ButterKnife) I was using the data tag before, but now stop using because our app code for updating the value is becoming little complicated and it is hard to debug.
The code I wrote above seems worked but it needs “unchecked cast”, so I wanted to know if you know the better solution. I have read about DataBindingEpoxyModel, but I thought you expected that it is used with the data tag. I was worried about that unexpected usage cause some problem like performance.
Thank you very much for polite response. It’s clear now. 👍
Did you look at https://github.com/airbnb/epoxy/wiki/Data-Binding-Support#custom-data-binding-models?
I think that can prevent you from having to make your own
BindingHolder
. It passes you the ViewDataBinding automatically