Jetpack Compose Academy · レッスン

安定して高速なリストのキー

変更後もComposeが項目を追跡できるようにします。

レッスン 3/413 ステップ

「安定して高速なリストのキー」はCoddyKit上の無料Jetpack Compose Academyレッスンです。 これはレッスン3/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはJetpack Compose Academy学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Jetpack Compose Academyコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

How Compose Tracks Rows

By default, Compose identifies each row by its position in the list. Item zero is item zero, item one is item one, and so on.

Position Breaks on Change

When you insert or remove a row, every item below shifts position. Compose now thinks those rows are different and rebuilds them needlessly.

The Cost of Confusion

Rebuilt rows lose their state and animations. A removed item can make the whole list flicker as Compose reshuffles everything below it.

Give Each Item a Key

A key is a stable, unique value that names each item by identity, not position. Then Compose follows items even as they move.

items(users, key = { it.id }) { user ->
    Text(user.name)
}

Use a Real Unique ID

The best key is a true unique id from your data, like a database id. It stays the same no matter where the item sits.

Keys Must Be Unique

Every key in the list must be different. Duplicate keys throw a runtime error, so never key items by a value two rows might share.

Don't Key by Index

Using the index as a key defeats the purpose, since indexes shift just like position. Reach for it only when you truly have no id.

State Stays With the Item

With stable keys, a row's remembered state travels with it. A checkbox stays checked even after items above it are deleted.

Keys Power Animations

Stable keys let Compose animate items moving, appearing, or leaving. Without them, it can't tell a move from a rebuild.

items(items, key = { it.id }) { item ->
    Text(item.label, Modifier.animateItem())
}

itemsIndexed Takes Keys Too

The itemsIndexed builder also accepts a key lambda. You still get the index, plus stable identity for every row.

itemsIndexed(data, key = { _, item -> item.id }) { i, item ->
    Text("$i: ${item.name}")
}

Keys Are Almost Always Worth It

For any list that can change, adding keys is a tiny effort with a big payoff: correct state, smooth animations, and fewer rebuilds.

Quick Check

What makes a good list key?

Recap: Stable Keys, Stable Lists

Keys give items identity beyond position. Use a real unique id, keep keys unique, and your state and animations just work. ✨

無料で開始

AI チューターと学ぶ Kotlin — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
30
レッスン
120

よくある質問

「安定して高速なリストのキー」レッスンは無料ですか?

はい。「安定して高速なリストのキー」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Jetpack Compose Academyコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Jetpack Compose Academyコースには全4レッスンが含まれています。

「安定して高速なリストのキー」で何を学びますか?

変更後もComposeが項目を追跡できるようにします。 ブラウザで直接実行するハンズオンコードでJetpack Compose Academyを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Jetpack Compose Academyを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのJetpack Compose Academyは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン3/4です。

「安定して高速なリストのキー」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このJetpack Compose Academyレッスンでコードを書いて実行できますか?

はい。すべてのJetpack Compose Academyレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. LazyColumnとスクロール可能なColumn
  2. itemsとitemsIndexed
  3. 安定して高速なリストのキー
  4. 固定ヘッダーとセクションリスト
← Jetpack Compose Academyに戻る