Lock in $30 Savings on PRO—Offer Ends Soon! ⏳
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Paging3のSeparatorsを使って LazyColumnにヘッダーや 別のアイテムを...
Search
kako351
December 16, 2023
Technology
0
730
Paging3のSeparatorsを使って LazyColumnにヘッダーや 別のアイテムを挿入する
このスライドは Android Advent Calendar 2023 16日目のスライドです。
https://qiita.com/advent-calendar/2023/android
kako351
December 16, 2023
Tweet
Share
More Decks by kako351
See All by kako351
Play Billing Library 7.0.0 変更点まとめ@potatotips#88
kako351
0
1.5k
CircleCIでFlakyなテストを再実行する_potatotips#83
kako351
0
200
ComposeでTimeRangePickerを作る_YUMEMI.grow Mobile #2
kako351
1
830
Composeの座標を取得する ~コーチマークにおける活用事例~_DroidKaigi.collect#1
kako351
2
3k
チームで導入する Jetpack Compose あの素晴らしいLTをもう一度.ver
kako351
1
1.3k
【DevFest & ADS JP 22】チームで導入するJetpackCompose@おいしい健康
kako351
0
2.5k
Other Decks in Technology
See All in Technology
1人1サービス開発しているチームでのClaudeCodeの使い方
noayaoshiro
2
560
フルカイテン株式会社 エンジニア向け採用資料
fullkaiten
0
9.9k
事業の財務責任に向き合うリクルートデータプラットフォームのFinOps
recruitengineers
PRO
2
180
Identity Management for Agentic AI 解説
fujie
0
370
ソフトウェアエンジニアとAIエンジニアの役割分担についてのある事例
kworkdev
PRO
0
120
ExpoのインダストリーブースでみたAWSが見せる製造業の未来
hamadakoji
0
190
【U/Day Tokyo 2025】Cygames流 最新スマートフォンゲームの技術設計 〜『Shadowverse: Worlds Beyond』におけるアーキテクチャ再設計の挑戦~
cygames
PRO
2
1.3k
Bedrock AgentCore Evaluationsで学ぶLLM as a judge入門
shichijoyuhi
1
150
さくらのクラウド開発ふりかえり2025
kazeburo
2
300
20251219 OpenIDファウンデーション・ジャパン紹介 / OpenID Foundation Japan Intro
oidfj
0
410
AI時代のワークフロー設計〜Durable Functions / Step Functions / Strands Agents を添えて〜
yakumo
3
1.9k
SREが取り組むデプロイ高速化 ─ Docker Buildを最適化した話
capytan
0
120
Featured
See All Featured
Six Lessons from altMBA
skipperchong
29
4.1k
Organizational Design Perspectives: An Ontology of Organizational Design Elements
kimpetersen
PRO
0
41
Design and Strategy: How to Deal with People Who Don’t "Get" Design
morganepeng
132
19k
How to optimise 3,500 product descriptions for ecommerce in one day using ChatGPT
katarinadahlin
PRO
0
3.4k
Facilitating Awesome Meetings
lara
57
6.7k
Documentation Writing (for coders)
carmenintech
77
5.2k
Noah Learner - AI + Me: how we built a GSC Bulk Export data pipeline
techseoconnect
PRO
0
72
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
26
3.3k
Building a Modern Day E-commerce SEO Strategy
aleyda
45
8.4k
Statistics for Hackers
jakevdp
799
230k
XXLCSS - How to scale CSS and keep your sanity
sugarenia
249
1.3M
[SF Ruby Conf 2025] Rails X
palkan
0
550
Transcript
insertSeparatorsで ヘッダーや別のアイテム付きの ページングリストを実装する kako351 Android Advent Calendar 2023
自己紹介 kako351 / @kako_351 Androidエンジニア • バイク(ハンターカブ) • ギター •
コーヒー自宅焙煎 趣味
本日話す内容 • Paging3の基本的な実装方法 • LazyColumnの基本的な実装方法 • LazyColumnのitemを利用する方法 話さないこと 話すこと •
Paging3のinsertSeparatorsについて • データの区別について • LazyColumnとの組み合わせ
ページングリスト中にヘッダーや別のアイテムを表示 できるようになる 例えば、左の画面のように リストアイテム: レシピ ヘッダー: 件数やソート機能 (上の赤枠) 途中: 別のレシピ検索提案(下の赤枠)
ゴール
まずはいつものようにPagingDataを作成します。 PagingData作成 val list = Pager( PagingConfig( pageSize = 10,
initialLoadSize = 10 ) ){ pagingSource }.flow.cachedIn(viewModelScope)
リストアイテムと他のアイテムを区別 する sealed class UiModel { data class Recipe( val
index: Int, val title: String ): UiModel() data class Header( val totalCount: Int ): UiModel() data class RelatedSearch( val keywords: List<String> ): UiModel() }
PagingDataのデータストリームを変換 する pager.flow.map { it.insertSeparators { before, after -> when
{ before == null -> UiModel.Header(999) after?.index == 2 -> UiModel.RelatedSearch(/* todo list */) else -> null } } }.cachedIn(viewModelScope)
insertSeparators 引数は以下の2つを持っています。 • terminalSeparatorType: TerminalSeparatorType • generator: suspend (before: T?,
after: T?) -> R? generator内で生成したセパレータと元の要素を含むPagingDataを返します。 https://developer.android.com/reference/kotlin/androidx/paging/PagingData#(androidx.paging.PagingData).insertSep arators(androidx.paging.TerminalSeparatorType,kotlin.coroutines.SuspendFunction2)
insertSeparators https://developer.android.com/reference/kotlin/androidx/paging/PagingData#(androidx.paging.PagingData).insertSep arators(androidx.paging.TerminalSeparatorType,kotlin.coroutines.SuspendFunction2) TerminalSeparatorType セパレータを表示するタイミングを設定するモード。2つある。 • FULLY_COMPLETE ◦ PagingSourceとRemoteMediatorの両方がページネーションの終わりに達したときにセパ レーターを表示する
• SOURCE_COMPLETE ◦ RemoteMediatorの状態に関係なく、PagingSourceがページネーションの最後に到達した時 点で、端末のセパレーター(ヘッダーとフッター)を表示する
insertSeparators https://developer.android.com/reference/kotlin/androidx/paging/PagingData#(androidx.paging.PagingData).insertSep arators(androidx.paging.TerminalSeparatorType,kotlin.coroutines.SuspendFunction2) generator: suspend (before: T?, after: T?) ->
R? 前後の要素を受け取ります。終端の場合はbefore、afterはそれぞれ nullになりま す。 リストが完全に空の場合はbefore、afterがともにnullになります。 つまり ヘッダーを追加したい場合は before == null で処理します。
LazyColumnにセパレータ付きのPagingDataを渡します。 paging-composeを利用します(※alpha版です 2023/07/21時点) UIに表示する val list = viewModel. list.collectAsLazyPagingItems ()
LazyColumn { items(count = list.itemCount, key = { it }) { index -> val item = list[index] ?: return@items when(item) { is UiModel.Header -> HeaderItem(item) is UiModel.Recipe -> RecipeItem(item) is UiModel.RelatedSearch -> RelatedSearchItems (item) } } } ところどころ雑なところはご了承ください......🙇
UIに表示する Paging3のSeparatorsを利用してリスト中にヘッダー や別のアイテムを挿入した画面が実装できました 🎉
まとめ • リスト中に別のアイテム(セパレータ)を挿入するにはinsertSeparatorsを使う • データを区別するには sealed classやsealed interface (interface)で可能 •
LazyColumn内でデータサブクラスごとに表示分けを行う
• データ ストリームを変換する https://developer.android.com/topic/libraries/architecture/paging/v3-t ransform?hl=ja#convert-ui-model • Paging: Getting fancy with
transformations, separators, headers, footers and search - MAD Skills https://www.youtube.com/watch?v=ZARz0pjm5YM 参考情報
ご静聴 ありがとう ございました 15 kako351