---
# System prepended metadata

title: Android RecyclerView を使用する 2021-08-04
tags: [Android, RecyclerView]

---

---
lang: ja-jp
breaks: true
---
# Android RecyclerView を使用する 2021-08-04

> RecyclerView で動的リストを作成する
> https://developer.android.com/guide/topics/ui/layout/recyclerview?hl=ja

> android/views-widgets-samples
> https://github.com/android/views-widgets-samples

> views-widgets-samples/RecyclerView/
> https://github.com/android/views-widgets-samples/tree/main/RecyclerView/

> views-widgets-samples/RecyclerViewSimple/
> https://github.com/android/views-widgets-samples/tree/main/RecyclerViewSimple

## app/build.gradle

```json=
dependencies {
    implementation 'com.android.support:recyclerview-v7:31.0.0'
}
```

## activity_main.xml

```xml=
<?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">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:id="@+id/recycler_view"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:scrollbars="vertical"
            app:layoutManager="LinearLayoutManager" />
    </FrameLayout>

</androidx.constraintlayout.widget.ConstraintLayout>
```

## recycler_item.xml

```xml=
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/recycler_item"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/textview"
        android:textSize="50sp" />
</FrameLayout>
```

## strings.xml

```xml=
<resources>
    <string name="app_name">RecyclerViewTest</string>
    <string name="textview">TextView</string>

    <!-- Add in flower names  -->
    <string-array name="flower_array">
        <item>アイビー</item>
        <item>アイビーゼラニウム</item>
        <item>アイリス</item>
        <item>葵</item>
        <item>アカシア</item>
        <item>アガパンサス</item>
        <item>アグリモニー</item>
        <item>アゲラタム</item>
        <item>朝顔</item>
        <item>アザミ</item>
        <item>アザレア</item>
        <item>紫陽花</item>
        <item>アスター</item>
        <item>アスペン</item>
        <item>アッツ桜</item>
        <item>アネモネ</item>
        <item>アベリア</item>
        <item>アマリリス</item>
        <item>あやめ</item>
        <item>アルストロメリア</item>
        <item>アロエ</item>
        <item>杏</item>
        <item>アンスリウム</item>
        <item>苺</item>
        <item>イチゴノキ</item>
        <item>イベリス</item>
        <item>インパチェンス</item>
        <item>ウィンターグリーン</item>
        <item>ウィンターコスモス</item>
        <item>うすべにあおい</item>
        <item>梅</item>
        <item>エーデルワイス</item>
        <item>エキナセア</item>
        <item>蝦夷菊</item>
        <item>エビネ</item>
        <item>エリカ</item>
        <item>エルム</item>
        <item>エレモフィラ</item>
        <item>オオイヌノフグリ</item>
        <item>オキザリス</item>
        <item>オーク</item>
        <item>オシロイバナ</item>
        <item>オステオスペルマム</item>
    </string-array>

</resources>
```

## RecyclerAdapter.java

```java=
package com.example.recyclerviewtest;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

public class RecyclerAdapter
        extends RecyclerView.Adapter<RecyclerAdapter.FlowerViewHolder> {

    private final String[] m_itemList;

    RecyclerAdapter(String[] itemList){
        m_itemList = itemList;
    }

    static class FlowerViewHolder
            extends RecyclerView.ViewHolder {

        private final TextView flowerTextView = itemView.findViewById(R.id.recycler_item);

        FlowerViewHolder(View itemView)
        {
            super(itemView);
        }

        public void bind(String word, int position) {
            StringBuilder sbr;
            sbr = new StringBuilder();
            sbr.append(position).append(":").append(word);
            flowerTextView.setText(sbr.toString());
        }
    }

    @NonNull
    @Override
    public FlowerViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_item, parent, false);
        return new FlowerViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull FlowerViewHolder holder, int position) {
        holder.bind(m_itemList[position], position);
    }

    @Override
    public int getItemCount() {
        return m_itemList.length;
    }
}
```

## MainActivity.java

```java=
package com.example.recyclerviewtest;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Retrieves data from datasource
        String[] flowerList = this.getResources().getStringArray(R.array.flower_array);

        RecyclerAdapter adapter = new RecyclerAdapter(flowerList);

        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setAdapter(adapter);
    }
}
```

## 実行


![](https://i.imgur.com/u5fSI4M.png)



###### tags: `Android` `RecyclerView`
