簡単にわかればいいという人向け。
RecyclerViewはListViewに変わる新しいビューで、いろんなサイトをみているとRecycleViewは面倒だとかListViewがあればいいという意見もあるが、AndroidStudioではレガシー扱いされてしまっているので、やはり今後はRecycleViewをなるべく使うようにしたほうがいいだろうと思われる。
RecyclerViewは3つのクラスが必要になる。
一つはデータを格納するだけのクラス。これはRecyclerViewの1行(1グリッド)に表示させるデータを保存しておくためのもの。データベースでいうRow、行に該当する。
二つ目は1行に表示されるレイアウトを保持するビューホルダークラス。
3つめはRecyclerViewとデータ、レイアウトのUI部品とを紐付けするアダプタークラス。
RecyclerViewではList<T>に格納されたデータを1行ずつ読み出し、レイアウトを作成しレイアウトのUI部品のインスタンスを取得しておき、そして実際にデータをUI部品に設定するような流れで行われている。
今回は試しにtextViewを二つだけ持つレイアウトを作成する。
このあたりは自分で好きなように作成して、List<T>に入れておけばいい。
viewには作成したレイアウトが入っているので、アクセスしてレイアウトのUI部品のインスタンスを所得することができるので、プロパティとしてインスタンスを保存しておく
layoutManagerは、どのように表示をさせるか決めるところで、垂直と水平にスクロールさせるLinearLayoutManagerとグリッドで表示するGridLayoutManager、スタッガード格子で表示するStaggeredGridLayoutManagerの3つがあるので、好みに応じて設定すればいい
RecyclerViewはListViewに変わる新しいビューで、いろんなサイトをみているとRecycleViewは面倒だとかListViewがあればいいという意見もあるが、AndroidStudioではレガシー扱いされてしまっているので、やはり今後はRecycleViewをなるべく使うようにしたほうがいいだろうと思われる。
RecyclerViewは3つのクラスが必要になる。
一つはデータを格納するだけのクラス。これはRecyclerViewの1行(1グリッド)に表示させるデータを保存しておくためのもの。データベースでいうRow、行に該当する。
二つ目は1行に表示されるレイアウトを保持するビューホルダークラス。
3つめはRecyclerViewとデータ、レイアウトのUI部品とを紐付けするアダプタークラス。
RecyclerViewではList<T>に格納されたデータを1行ずつ読み出し、レイアウトを作成しレイアウトのUI部品のインスタンスを取得しておき、そして実際にデータをUI部品に設定するような流れで行われている。
今回は試しにtextViewを二つだけ持つレイアウトを作成する。
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/crConstraintLayout"
android:layout_width="match_parent"
android:layout_height="40dp"
tools:background="#aaaaaa">
<TextView
android:id="@+id/tvName"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@id/btnButton"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@id/tvPrice"
app:layout_constraintTop_toTopOf="@id/btnButton" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="@id/btnButton"
app:layout_constraintLeft_toRightOf="@id/tvName"
app:layout_constraintRight_toLeftOf="@id/btnButton"
app:layout_constraintTop_toTopOf="@id/btnButton" />
</android.support.constraint.ConstraintLayout>
アクティビティはこのように設置している。
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rvRecyclerView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
まずはデータを保存するクラスを作成するこのあたりは自分で好きなように作成して、List<T>に入れておけばいい。
class RowData {
var name: String = ""
var price: Int = 0
}
次にレイアウトのUI部品のインスタンスを保持するクラスを作成する。viewには作成したレイアウトが入っているので、アクセスしてレイアウトのUI部品のインスタンスを所得することができるので、プロパティとしてインスタンスを保存しておく
class RecycleViewHolder(val view: View) : RecyclerView.ViewHolder(view) {
val tvName: TextView = view.findViewById(R.id.tvName)
val tvPrice: TextView = view.findViewById(R.id.tvPrice)
}
次に実際にレイアウトを生成し実際にデータをバインドするためのクラスを作る。
class RecycleViewAdapter(val list: List<RowData>) : RecyclerView.Adapter<RecycleViewHolder>() {
//1行表示するたびに呼ばれる レイアウトを生成してViewHolderを生成して返す
//1行おきに表示を変えたいとか、10行ごとに違う表示をしたい場合は、ここでviewTypeに従って処理を分ける
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecycleViewHolder {
return RecycleViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.recycle_layout, parent, false))
}
//List<T>に入っているデータ数を返す
override fun getItemCount(): Int {
return list.count()
}
//1行表示するたびに呼ばれるので、ここでレイアウトのUI部品にデータを入れる
override fun onBindViewHolder(holder: RecycleViewHolder, position: Int) {
holder.tvName.text = list[position].name
holder.tvPrice.text = list[position].price.toString()
}
}
アクティビティでは下記のようにRecyclerViewにアダプタをバインドする。layoutManagerは、どのように表示をさせるか決めるところで、垂直と水平にスクロールさせるLinearLayoutManagerとグリッドで表示するGridLayoutManager、スタッガード格子で表示するStaggeredGridLayoutManagerの3つがあるので、好みに応じて設定すればいい
//テストとなるデータを作成する
var list: MutableList<RowData> = mutableListOf()
for (i: Int in 1..30) {
val rowData: RowData = RowData().also {
it.name = i.toString()
it.price = i * 10
}
list.add(rowData)
}
//RecyclerViewを取得してアダプタをバインドする
val recyclerView: RecyclerView = findViewById(R.id.rvRecyclerView)
recyclerView.adapter = RecycleViewAdapter(list)
//RecyclerViewの表示方法を設定する
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false)