https://kotlinlang.org logo
#android
Title
# android
h

Hong

06/05/2018, 6:22 PM
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

Doru N.

06/05/2018, 7:29 PM
I need to import those manually, right? As by default, it will import only from one layout.
h

Hong

06/05/2018, 7:32 PM
Android Studio will import automatically I believe
s

sindrenm

06/06/2018, 5:21 AM
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

Doru N.

06/06/2018, 5:52 AM
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

sindrenm

06/06/2018, 6:08 AM
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
2 Views