0
2

More than 3 years have passed since last update.

【Android】RecyclerViewの使い方メモ

Last updated at Posted at 2020-08-23

RecyclerViewの作り方

  • RecyclerViewを使うたびに忘れるので、メモ用にまとめます。

手順

  1. 依存関係の追加
  2. 記述するもの

依存関係の追加

  • まずは依存関係の追加。他の記事だと、v7のサポートライブラリを使ってるものがありますが、API 29からdeprecatedされてるので、androidxを入れましょう。
build.gradle
implementation 'androidx.recyclerview:recyclerview:1.1.0'

工程

  • layout

    • RecyclerViewのレイアウト
    • listの1個のレイアウトファイル
  • RecyclerViewのAdapaterクラス

  • ViewHolderクラス

  • layoutManeger

layout

  • RecyclerViewのレイアウト

  • サンプルコード

<?xml version="1.0" encoding="utf-8"?>
<LinerLayout 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"
    tools:context=".view.MainMenuFragment">

    // RecyclerView
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinerLayoutt>
  • listの1個のレイアウトファイル

    • リストアイテムのレイアウトをlayoutフォルダに作成します。
    • 特にコードで何か特別なことをしないといけない訳ではない。
  • サンプルコード

    <androidx.appcompat.widget.LinearLayoutCompat 
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <ImageView
            android:id="@+id/image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@android:drawable/alert_dark_frame" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="20dp" />

    </androidx.appcompat.widget.LinearLayoutCompat>

コード

  • RecyclerViewのAdapaterクラスの作成

    • Adapterとは、のちに出てくるViewHolderの作成や入れ替えなどを担当するクラス。
  • サンプルコード

class RecyclerViewAdapter(val list: List<Item>) : RecyclerView.Adapter<RecyclerViewAdapter.RecyclerViewHolder>() {

    class RecyclerViewHolder(view: View): RecyclerView.ViewHolder(view) {
        val imageView = view.findViewById<ImageView>(R.id.image)
        val NameText = view.findViewById<TextView>(R.id.name)
    }

    // ViewHolderを作成している。
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {
        val rowView = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
        return RecyclerViewHolder(rowView)
    }

    // ViewHolderの中身を変更。
    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {

        holder.nameText.text = list[position].name
    }

    override fun getItemCount(): Int = list.size
}
  • Activity側の処理

class MainActivity : AppCompatActivity() {

    private lateinit var recyclerView: RecyclerView
    private lateinit var viewAdapter: RecyclerView.Adapter<*>
    private lateinit var layoutManager: RecyclerView.LayoutManager

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        layoutManager = LinearLayoutManager(this)
        viewAdapter = RestaurantListAdapter(list)
        recyclerView = view.findViewById<RecyclerView>(R.id.restaurant_list).also {
            it.layoutManager = layoutManager
            it.adapter = viewAdapter
        }
    }
}

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2