<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:listitem="@layout/list_item" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:textAppearance="@style/TextAppearance.AppCompat.Large"
android:textSize="30sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/is_married"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Name" />
<TextView
android:id="@+id/age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
app:layout_constraintBottom_toTopOf="@+id/is_married"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/name"
tools:text="age" />
<TextView
android:id="@+id/is_married"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="@+id/name"
app:layout_constraintEnd_toEndOf="@+id/age"
app:layout_constraintTop_toBottomOf="@+id/age"
tools:text="isMarried" />
</androidx.constraintlayout.widget.ConstraintLayout>
<resources>
……
<string name="married">Married</string>
<string name="un_married">Un married</string>
</resources>
package domain.recyclerviewsample
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import androidx.recyclerview.widget.LinearLayoutManager
import red.mercy.recyclerviewsample.databinding.ActivityMainBinding
class MainActivity : AppCompatActivity() {
private lateinit var binding: ActivityMainBinding
// Item がクリックされた時に実行する処理(interface) を実装する
private val onItemClick = object : MyAdapter.Callback {
override fun onItemClick(person: Person) {
Toast.makeText(applicationContext, person.toString(), Toast.LENGTH_SHORT).show()
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)
val persons = arrayListOf(
Person("Bob", 23, false),
Person("John", 25, true),
Person("Nancy", 22, true),
Person("Maria", 20, false),
Person("Mirror", 28, false),
Person("David", 27, true),
Person("Ada", 26, true)
)
val viewManager = LinearLayoutManager(this)
// 実装した interface を Adapter のコンストラクタ引数として渡す
val myAdapter = MyAdapter(this, persons, onItemClick)
binding.list.apply {
layoutManager = viewManager
adapter = myAdapter
}
}
}
package domain.recyclerviewsample
import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import red.mercy.recyclerviewsample.databinding.ListItemBinding
class MyAdapter(
private val context: Context,
private val persons: ArrayList<Person>,
private val callback: Callback
) : RecyclerView.Adapter<MyAdapter.ViewHolder>() {
// Item がクリックされた時に呼び出す処理を
// interface として定義
interface Callback {
fun onItemClick(person: Person)
}
private val inflater = LayoutInflater.from(context)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder =
ViewHolder(context, ListItemBinding.inflate(inflater, parent, false), callback)
override fun onBindViewHolder(holder: ViewHolder, position: Int) = holder.bind(persons[position])
override fun getItemCount(): Int = persons.size
class ViewHolder(
private val context: Context,
private val binding: ListItemBinding,
private val callback: Callback
) : RecyclerView.ViewHolder(binding.root) {
fun bind(person: Person) {
binding.run {
// ItemView の OnClickListener で、interface のメソッドを呼び出す
root.setOnClickListener {
callback.onItemClick(person)
}
name.text = person.name
age.text = person.age.toString()
isMarried.text = context.getString(if (person.isMarried) R.string.married else R.string.un_married)
}
}
}
}
package domain.recyclerviewsample
import java.io.Serializable
data class Person(
val name: String,
val age: Int,
val isMarried: Boolean = false
) : Serializable {
override fun toString(): String =
"name: $name, age: $age, ${if (isMarried) "Married" else "Un married"}"
}