Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AAC Paging & Kotlin化の紹介

AAC Paging & Kotlin化の紹介

Koji Wakamiya

November 27, 2019
Tweet

More Decks by Koji Wakamiya

Other Decks in Technology

Transcript

  1. 自己紹介 • Koji Wakamiya ◦ Github https://github.com/koji-1009 ◦ Blog https://blog.dr1009.com/

    • Studyplus株式会社 ◦ Androidアプリケーションエンジニア ◦ iOSアプリ開発にも参加中 ◦ Flutterのもくもく会とかもときたま開催 • デスストとポケモンとリングフィットの並走中 4
  2. Pagingライブラリとは • Android Architecture Components 勉強会 #6 ◦ https://gdg-tokyo.connpass.com/event/127484/ ◦

    http://yaraki.github.io/slides/aac-paging/index.html • Codelab ◦ https://codelabs.developers.google.com/codelabs/android-paging/ • PagingSample(Google architecture-components-samples) ◦ https://github.com/android/architecture-components-samples/tree/master/PagingSample • PagingWithNetworkSample(Google architecture-components-samples) ◦ https://github.com/android/architecture-components-samples/tree/master/PagingWithNetworkSample 7
  3. Pagingライブラリなし class HogeViewModel(private val repository: FugaRepository) : ViewModel() { val

    items: LiveData<List<Item>> = cacheLiveData private val chachLiveData: MutableLiveData<List<Item>> = MutableLiveData(emptyList()) private val cacheList: List<Item> = mutableList() private var currentPage = 0 fun fetchNext() { ~~~ } } 9
  4. Pagingライブラリあり • FugaRepositoryでやること ◦ Repository内でDataSource.Factoryを作る ▪ PageKeyedDataSource / ItemKeyedDataSource /

    PositionalDataSource のいずれかをAPIや用途によって選択する ◦ DataSource.Factory内で読み込みタイミングに応じたabstract methodを記述する ▪ PageKeyedDataSourceとItemKeyedDataSourceなら下記3つ • loadInitial : 初回読み込み、SwipeRefresh • loadBefore : リストの一番上に戻ったときの追加読み込み • loadAfter : リストの一番下に行った時の追加読み込み ▪ PositionalDataSourceはInitialとRangeの読み込み概念のみ 11
  5. Pagingの仕組み(概略) 13 1. AbstractListをPagedListをPagedListAdapterが受け取る a. PagedListAdapterを継承したAdapterをRecyclerViewにセットする b. PagedListAdapterのgetItemはnullable (なおListAdapterはnon-null) 2.

    PagedListAdapterがPagedListにスクロール状態を伝える a. PagedListAdapterがPagedListのgetメソッドを呼び出す 3. PagedListのgetメソッドが要素かnullを返す a. データを保持している、読み込み終わっているなら要素を返す b. データを破棄している、まだ読み込んでいないなら読み込みを開始する i. 読み込み終わったら、 RecyclerViewのDataAdapterObserverにinsert通知