it will have multiple imports, something like: ```...
# android
h
it will have multiple imports, something like:
Copy code
import kotlinx.android.synthetic.main.my_view_1.view.*
import kotlinx.android.synthetic.main.my_view_2.view.*
import kotlinx.android.synthetic.main.card_view.view.*
d
I need to import those manually, right? As by default, it will import only from one layout.
h
Android Studio will import automatically I believe
s
If you already have imported views from, say, _my_view_1.xml_, and there are IDs in there that match what you're trying to do, then Studio and Kotlin will just assume that this is the view you wanna use. To get around this, I've personally given IDs to my views related to their contexts as well, e.g.,
textViewProfileUsername
.
Also, if I recall correctly, I believe you'll get an error if you're synthetically importing views from multiple sources with the same IDs, either during the import statements or when you're trying to use the them (probably the latter).
d
Yes, looks like what @sindrenm said is correct, I get the error when trying to import from multiple layouts.
So what are my choices, when dealing with my case of having a BaseViewHolder which models the common information of multiple ViewHolders ?
Am I forced to bind the Views, the old way? (findViewById) ?
s
Well, technically, as far as I know, it doesn't really matter where you import from. The only reason for the imports is so that the synthetic imports can know what type the ID should refer to. So if you have a
<TextView id="@+id/usernameTextView" />
in both _my_view_1.xml_ and in _my_view_2.xml_, as long as both of them are `TextView`s, I believe you can import from whichever you want. 🤔
But that might sound a little bit hacky, I guess …
Since in the
ViewHolder
, you'll access the view from
itemView
anyway, right? So it's essentially which view
itemView
refers to that matters.
👍 1