问题
Normally, when I need to include the same layout in one screen, I would wrap this layout in two different containers with a different id and call findViewById
on those two parents but I don't know if and how I could achieve the same result with kotlin's syntethic
properties. Just to be more clear:
I am in the situation where fragment_xyz.xml is composed like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="@+id/include1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<include layout="@layout/item_detail" />
</FrameLayout>
<FrameLayout
android:id="@+id/include2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<include layout="@layout/item_detail" />
</FrameLayout>
</LinearLayout>
item_detail.xml'content is similar to the following:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
And I would like to access those title and description with the usual:
import kotlinx.android.synthetic.main.item_detail.*
But I need to distinguish between the one inside FrameLayout include1 and FrameLayout include2
Thanks in advance!
Edit: typos.
回答1:
You need import:
import kotlinx.android.synthetic.main.fragment_xyz.*
Then get the title and description from framelayout
include1
and include2
like below:
val title1 by lazy {
include1.title
}
val description1 by lazy {
include1.description
}
val title2 by lazy {
include2.title
}
val description2 by lazy {
include2.description
}
来源:https://stackoverflow.com/questions/51845807/how-to-use-syntethic-properties-when-you-have-the-same-layout-included-twice