基本的には findViewById
と同じ
以下のようなレイアウトファイルを使った場合、
ViewHolderの itemView
に生えている対象のビューを捕まえてあげるだけです。
view_text.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
holder.itemView.apply {
textView.text = context.getString(R.string.app_name)
}
findViewById
を使った場合は以下のようになるので、
同じことをやっていると考えればとくに難しいことはないでしょう。
holder.itemView.apply {
findViewById<TextView>(R.id.textView).text = context.getString(R.string.app_name)
}
そのため、レイアウトファイルをincludeしたものも同じように扱えばよいです。