Paging 程式庫總覽
Android Jetpack 的一部分。
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
Paging 程式庫可協助您從本機儲存空間或網路載入較大的資料集並以多頁形式顯示資料。此方法可讓應用程式更有效率地使用網路頻寬和系統資源。Paging 程式庫的元件設計成配合建議的 Android 應用程式架構,可與其他 Jetpack 元件完美整合,並提供最優質的 Kotlin 支援。
使用 Paging 程式庫的好處
Paging 程式庫包含下列功能:
- 針對分頁資料的記憶體內快取。這有助於確保應用程式在使用分頁資料時,更有效率地使用系統資源。
- 內建要求簡化功能,可確保應用程式有效率地使用網路頻寬和系統資源。
- 可設定的
RecyclerView
轉接程式,在使用者捲動至「載入資料」的底部時,自動要求資料。 - 優質的 Kotlin 協同程式和流程支援,以及
LiveData
和 RxJava。 - 內建錯誤處理機制支援,包括重新整理和重試功能。
提供意見
您的意見可協助我們改善 Jetpack。如果您發現新的問題或有改善此程式庫的想法時,請告訴我們。請先查看這個程式庫的現有問題,再建立新的問題。只要按一下星號按鈕,即可對現有問題新增投票。
如要進一步瞭解如何提交意見回饋,請參閱 Issue Tracker 說明文件。
設定
如要將 Paging 元件匯入 Android 應用程式,請在應用程式的 build.gradle
檔案中新增下列依附元件:
dependencies {
def paging_version = "3.3.2"
implementation "androidx.paging:paging-runtime:$paging_version"
// alternatively - without Android dependencies for tests
testImplementation "androidx.paging:paging-common:$paging_version"
// optional - RxJava2 support
implementation "androidx.paging:paging-rxjava2:$paging_version"
// optional - RxJava3 support
implementation "androidx.paging:paging-rxjava3:$paging_version"
// optional - Guava ListenableFuture support
implementation "androidx.paging:paging-guava:$paging_version"
// optional - Jetpack Compose integration
implementation "androidx.paging:paging-compose:3.3.2"
}
dependencies {
val paging_version = "3.3.2"
implementation("androidx.paging:paging-runtime:$paging_version")
// alternatively - without Android dependencies for tests
testImplementation("androidx.paging:paging-common:$paging_version")
// optional - RxJava2 support
implementation("androidx.paging:paging-rxjava2:$paging_version")
// optional - RxJava3 support
implementation("androidx.paging:paging-rxjava3:$paging_version")
// optional - Guava ListenableFuture support
implementation("androidx.paging:paging-guava:$paging_version")
// optional - Jetpack Compose integration
implementation("androidx.paging:paging-compose:3.3.2")
}
程式庫架構
Paging 程式庫的元件會在應用程式的三個層執行:
- 存放區層
ViewModel
層- 使用者介面層
本節會說明在各層執行的 Paging 程式庫元件,以及這些元件如何搭配運作,藉此載入並顯示分頁資料。
存放區層
存放區層的主要 Paging 程式庫元件為 PagingSource
。每個 PagingSource
物件都會定義資料來源,以及如何從該來源擷取資料。PagingSource
物件可以從任何單一來源載入資料,包括網路來源和本機資料庫。
另一個您可以使用的Paging 程式庫元件為 RemoteMediator
。RemoteMediator
物件可處理分層資料來源的分頁,例如具有本機資料庫快取的網路資料來源。
ViewModel 層
以 PagingSource
物件及PagingConfig
設定物件為基礎,Pager
元件提供用於建構在回應串流中傳送 PagingData
執行個體的公開 API。
將 ViewModel
層連結至使用者介面的元件為 PagingData
。PagingData
物件是分頁資料快照的容器。查詢 PagingSource
物件並儲存結果。
使用者介面層
使用者介面層的主要 Paging 程式庫元件為 PagingDataAdapter
,處理分頁資料的RecyclerView
轉接程式。
或者,您可以使用隨附的 AsyncPagingDataDiffer
元件來建立專屬的自訂轉接程式。
其他資源
如要進一步瞭解 Paging 程式庫,請參閱以下資源:
程式碼研究室
為你推薦
載入並顯示分頁資料
Discover the latest app development tools, platform updates, training, and documentation for developers across every Android device.
遷移至 Paging 3
Discover the latest app development tools, platform updates, training, and documentation for developers across every Android device.
網路和資料庫的網頁
Discover the latest app development tools, platform updates, training, and documentation for developers across every Android device.