Hi guys, I have a question. Let's say I have a Vie...
# android
l
Hi guys, I have a question. Let's say I have a View Holder for RecyclerView and I have 2 layouts with the same views and same names, just different sizes of views. How can I use synthetic with kotlin ?
b
Great question, this is one of the few edge cases where I think findViewById() would actually be a better option. Would love to get other opinions on this.
l
Maybe the creator or @jw could help us out here ? Do you know what would be cool ? If we could override those views in this style
j
I've never used the synthetic properties and I find them to be the wrong solution to the larger problem
l
Okay, so how do you propose solving this case ? Dagger or is there something else ?
j
Sounds like
findViewById
solves the problem
l
Well, okay. I thought there are other ways of solving this. But thank you for your answer
b
@jw I would like to know why synthetic is a wrong way to go. I loved Butterknife, but not needing to use any annotation at all is really making my life more fun. I even have a generic ViewHolder that takes a view and I don’t have to findViewById or inject anything. It makes coding so much easier. And afaik it is just lazy-loading the views using
findViewById
You will probably make a good point that will make me stop using synthetic properties…so I migh (probably) regret this question 🤐
j
Databinding has a superior approach as it generates a type per layout (including across resource configurations) with properties for all contained views. This means no need for a magic cache behind the scenes and no need for magic imports. It also optimizes the lookup requiring only a single traversal over the view tree instead of one per view. When a view is only included in one configuration but the other it's marked as nullable. When you use different types across configurations the property reflects the common supertype.
2
Unfortunately Databinding also includes databinding which I don't really like.
☝️ 3
😂 5
😀
b
😆 yep, not a fan of databinding either
It is probably more efficient, but in real case scenario, I rather have easier coding than needing to write more code (variable declarations in XML and stuff, just don’t like it)
the performance penalty is probably not interesting
because that is what I guess you’re pointing at, that it costs performance
j
Right. Which is why i'd like the layout codegen part of databinding to be separate somehow. Because I'd rather do like
Copy code
val layout = LayoutName(context)
layout.name.text = "hey"
etc.
👍 2
b
that would be great!
Hope you are able to make this happen 😉