Organiza tus páginas con colecciones
Guarda y categoriza el contenido según tus preferencias.
Esta guía se basa en la Descripción general de la biblioteca de Paging y explica cómo puedes presentar listas de información a los usuarios en la IU de tu app, en especial cuando esta información cambia.
classConcertActivity:AppCompatActivity(){privatevaladapter=ConcertAdapter()// Use the 'by viewModels()' Kotlin property delegate// from the activity-ktx artifactprivatevalviewModel:ConcertViewModelbyviewModels()overridefunonCreate(savedInstanceState:Bundle?){super.onCreate(savedInstanceState);viewModel.concerts.observe(this,Observer{adapter.submitList(it)})}}
Como las fuentes de datos proporcionan instancias nuevas de PagedList, la actividad envía estos objetos al adaptador. La implementación de PagedListAdapter define cómo se procesan las actualizaciones y administra automáticamente la paginación y el diffing de listas. Por lo tanto, tu ViewHolder solo necesita vincularse a un elemento proporcionado en particular:
Kotlin
classConcertAdapter():PagedListAdapter<Concert,ConcertViewHolder>(DIFF_CALLBACK){overridefunonBindViewHolder(holder:ConcertViewHolder,position:Int){valconcert:Concert? =getItem(position)// Note that "concert" is a placeholder if it's null.holder.bindTo(concert)}companionobject{privatevalDIFF_CALLBACK=...// See Implement the diffing callback section.}}
Java
publicclassConcertAdapterextendsPagedListAdapter<Concert,ConcertViewHolder>{protectedConcertAdapter(){super(DIFF_CALLBACK);}@OverridepublicvoidonBindViewHolder(@NonNullConcertViewHolderholder,intposition){Concertconcert=getItem(position);// Note that "concert" can be null if it's a placeholder.holder.bindTo(concert);}privatestaticDiffUtil.ItemCallback<Concert>DIFF_CALLBACK=...// See Implement the diffing callback section.}
El objeto PagedListAdapter administra los eventos de carga de páginas con un objeto PagedList.Callback. A medida que el usuario se desplaza, el PagedListAdapter llama a PagedList.loadAround() para proporcionar sugerencias a la PagedList subyacente en cuanto a los elementos que debe recuperar de DataSource.
Implementa la devolución de llamada de diffing
En el siguiente ejemplo, se muestra una implementación manual de areContentsTheSame() que compara los campos de objetos relevantes:
Kotlin
privatevalDIFF_CALLBACK=object:DiffUtil.ItemCallback<Concert>(){// The ID property identifies when items are the same.overridefunareItemsTheSame(oldItem:Concert,newItem:Concert)=oldItem.id==newItem.id// If you use the "==" operator, make sure that the object implements// .equals(). Alternatively, write custom data comparison logic here.overridefunareContentsTheSame(oldItem:Concert,newItem:Concert)=oldItem==newItem}
Java
privatestaticDiffUtil.ItemCallback<Concert>DIFF_CALLBACK=newDiffUtil.ItemCallback<Concert>(){@OverridepublicbooleanareItemsTheSame(ConcertoldItem,ConcertnewItem){// The ID property identifies when items are the same.returnoldItem.getId()==newItem.getId();}@OverridepublicbooleanareContentsTheSame(ConcertoldItem,ConcertnewItem){// Don't use the "==" operator here. Either implement and use .equals(),// or write custom data comparison logic here.returnoldItem.equals(newItem);}};
Como el adaptador incluye tu definición de elementos de comparación, este detecta automáticamente los cambios en esos elementos cuando se carga un objeto PagedList nuevo. Como resultado, el adaptador activa animaciones de elementos eficaces en el objeto RecyclerView.
Haz diffing usando un tipo de adaptador diferente
Si decides no heredar de PagedListAdapter, por ejemplo, cuando usas una biblioteca que proporciona su propio adaptador, puedes usar la función de diffing del adaptador de la biblioteca de Paging trabajando directamente con un objeto AsyncPagedListDiffer.
Proporciona marcadores de posición en tu IU
En los casos en que desees que la IU muestre una lista antes de que la app haya terminado de recuperar datos, puedes mostrarles elementos de la lista de marcadores de posición a los usuarios. La PagedList controla este caso presentando los datos de elementos de la lista como null hasta que se cargan los datos.
Los marcadores de posición tienen los siguientes beneficios:
Compatibilidad con barras de desplazamiento: La PagedList proporciona la cantidad de elementos que contiene la lista al PagedListAdapter. Esta información permite que el adaptador dibuje una barra de desplazamiento que transmite el tamaño original de la lista. A medida que se cargan las páginas nuevas, la barra de desplazamiento no salta porque tu lista no cambia de tamaño.
No se necesita ningún ícono giratorio de carga: Debido a que el tamaño de la lista ya es conocido, no es necesario avisarles a los usuarios que se están cargando más elementos. Los marcadores de posición transmiten esa información.
Sin embargo, antes de agregar compatibilidad con marcadores de posición, ten en cuenta las siguientes condiciones previas:
Se requiere un conjunto de datos contables: Las instancias de DataSource de la biblioteca de persistencias Room pueden contar sus elementos de manera eficiente. Sin embargo, si usas una solución de almacenamiento local personalizada o una arquitectura de datos solo de red, puede ser costoso o incluso imposible determinar cuántos elementos componen tu conjunto de datos.
Se requiere un adaptador para tener en cuenta los elementos descargados: El adaptador o mecanismo de presentación que utilices para preparar la lista para el aumento debe manejar elementos de lista nulos. Por ejemplo, cuando vinculas datos a un ViewHolder, debes proporcionar valores predeterminados para representar los datos descargados.
Se requieren vistas de elementos del mismo tamaño: Si los tamaños de los elementos de la lista pueden cambiar en función de su contenido, como las actualizaciones de las redes sociales, el encadenado entre elementos no se verá bien. Sugerimos inhabilitar los marcadores de posición en este caso.
Envía comentarios
Usa estos recursos para compartir tus comentarios y tus ideas con nosotros:
El contenido y las muestras de código que aparecen en esta página están sujetas a las licencias que se describen en la Licencia de Contenido. Java y OpenJDK son marcas registradas de Oracle o sus afiliados.
Última actualización: 2025-07-27 (UTC)
[[["Fácil de comprender","easyToUnderstand","thumb-up"],["Resolvió mi problema","solvedMyProblem","thumb-up"],["Otro","otherUp","thumb-up"]],[["Falta la información que necesito","missingTheInformationINeed","thumb-down"],["Muy complicado o demasiados pasos","tooComplicatedTooManySteps","thumb-down"],["Desactualizado","outOfDate","thumb-down"],["Problema de traducción","translationIssue","thumb-down"],["Problema con las muestras o los códigos","samplesCodeIssue","thumb-down"],["Otro","otherDown","thumb-down"]],["Última actualización: 2025-07-27 (UTC)"],[],[],null,["# Display paged lists\n\nThis guide builds upon the [Paging Library\noverview](/topic/libraries/architecture/paging), describing how you can\npresent lists of information to users in your app's UI, particularly when this\ninformation changes.\n| **Caution:** This guide covers an older, deprecated version of the Paging library. For more information about the latest stable version of Paging, see the [Paging\n| 3 guides](/topic/libraries/architecture/paging/v3-overview).\n\nConnect your UI to your view model\n----------------------------------\n\nYou can connect an instance of\n[`LiveData\u003cPagedList\u003e`](/reference/androidx/lifecycle/LiveData) to a\n[`PagedListAdapter`](/reference/androidx/paging/PagedListAdapter), as shown\nin the following code snippet: \n\n### Kotlin\n\n```kotlin\nclass ConcertActivity : AppCompatActivity() {\n private val adapter = ConcertAdapter()\n\n // Use the 'by viewModels()' Kotlin property delegate\n // from the activity-ktx artifact\n private val viewModel: ConcertViewModel by viewModels()\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState);\n viewModel.concerts.observe(this, Observer { adapter.submitList(it) })\n }\n}\n```\n\n### Java\n\n```java\npublic class ConcertActivity extends AppCompatActivity {\n private ConcertAdapter adapter = new ConcertAdapter();\n private ConcertViewModel viewModel;\n\n @Override\n public void onCreate(@Nullable Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n viewModel = new ViewModelProvider(this).get(ConcertViewModel.class);\n viewModel.concertList.observe(this, adapter::submitList);\n }\n}\n```\n\nAs data sources provide new instances of\n[`PagedList`](/reference/androidx/paging/PagedList), the activity sends\nthese objects to the adapter. The\n[`PagedListAdapter`](/reference/androidx/paging/PagedListAdapter)\nimplementation defines how updates are computed, and it automatically handles\npaging and list diffing. Therefore, your\n[`ViewHolder`](/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder)\nonly needs to bind to a particular provided item: \n\n### Kotlin\n\n```kotlin\nclass ConcertAdapter() :\n PagedListAdapter\u003cConcert, ConcertViewHolder\u003e(DIFF_CALLBACK) {\n override fun onBindViewHolder(holder: ConcertViewHolder, position: Int) {\n val concert: Concert? = getItem(position)\n\n // Note that \"concert\" is a placeholder if it's null.\n holder.bindTo(concert)\n }\n\n companion object {\n private val DIFF_CALLBACK = ... // See #implement-diffing-callback section.\n }\n}\n```\n\n### Java\n\n```java\npublic class ConcertAdapter\n extends PagedListAdapter\u003cConcert, ConcertViewHolder\u003e {\n protected ConcertAdapter() {\n super(DIFF_CALLBACK);\n }\n\n @Override\n public void onBindViewHolder(@NonNull ConcertViewHolder holder,\n int position) {\n Concert concert = getItem(position);\n\n // Note that \"concert\" can be null if it's a placeholder.\n holder.bindTo(concert);\n }\n\n private static DiffUtil.ItemCallback\u003cConcert\u003e DIFF_CALLBACK\n = ... // See #implement-diffing-callback section.\n}\n```\n\nThe [`PagedListAdapter`](/reference/androidx/paging/PagedListAdapter)\nhandles page load events using a\n[`PagedList.Callback`](/reference/androidx/paging/PagedList.Callback)\nobject. As the user scrolls, the `PagedListAdapter` calls\n[`PagedList.loadAround()`](/reference/androidx/paging/PagedList#loadaround)\nto provide hints to the underlying\n[`PagedList`](/reference/androidx/paging/PagedList) as to\nwhich items it should fetch from the\n[`DataSource`](/reference/androidx/paging/DataSource).\n| **Note:** [`PagedList`](/reference/androidx/paging/PagedList) is content-immutable. This means that, although new content can be loaded into an instance of `PagedList`, the loaded items themselves cannot change once loaded. As such, if content in a `PagedList` updates, the [`PagedListAdapter`](/reference/androidx/paging/PagedListAdapter) object receives a completely new `PagedList` that contains the updated information.\n\nImplement the diffing callback\n------------------------------\n\nThe following sample shows a manual implementation of\n[`areContentsTheSame()`](/reference/androidx/recyclerview/widget/DiffUtil.ItemCallback#arecontentsthesame),\nwhich compares relevant object fields: \n\n### Kotlin\n\n```kotlin\nprivate val DIFF_CALLBACK = object : DiffUtil.ItemCallback\u003cConcert\u003e() {\n // The ID property identifies when items are the same.\n override fun areItemsTheSame(oldItem: Concert, newItem: Concert) =\n oldItem.id == newItem.id\n\n // If you use the \"==\" operator, make sure that the object implements\n // .equals(). Alternatively, write custom data comparison logic here.\n override fun areContentsTheSame(\n oldItem: Concert, newItem: Concert) = oldItem == newItem\n}\n```\n\n### Java\n\n```java\nprivate static DiffUtil.ItemCallback\u003cConcert\u003e DIFF_CALLBACK =\n new DiffUtil.ItemCallback\u003cConcert\u003e() {\n\n @Override\n public boolean areItemsTheSame(Concert oldItem, Concert newItem) {\n // The ID property identifies when items are the same.\n return oldItem.getId() == newItem.getId();\n }\n\n @Override\n public boolean areContentsTheSame(Concert oldItem, Concert newItem) {\n // Don't use the \"==\" operator here. Either implement and use .equals(),\n // or write custom data comparison logic here.\n return oldItem.equals(newItem);\n }\n};\n```\n\nBecause your adapter includes your definition of comparing items, the adapter\nautomatically detects changes to these items when a new `PagedList` object is\nloaded. As a result, the adapter triggers efficient item animations within your\n`RecyclerView` object.\n\nDiffing using a different adapter type\n--------------------------------------\n\nIf you choose not to inherit from\n[`PagedListAdapter`](/reference/androidx/paging/PagedListAdapter)---such as\nwhen you're using a library that provides its own adapter---you can still use the\nPaging Library adapter's diffing functionality by working directly with an\n[`AsyncPagedListDiffer`](/reference/androidx/paging/AsyncPagedListDiffer)\nobject.\n\nProvide placeholders in your UI\n-------------------------------\n\nIn cases where you want your UI to display a list before your app has finished\nfetching data, you can show placeholder list items to your users. The\n[`PagedList`](/reference/androidx/paging/PagedList) handles this case by\npresenting the list item data as `null` until the data is loaded.\n| **Note:** By default, the Paging Library enables this placeholder behavior.\n\nPlaceholders have the following benefits:\n\n- **Support for scrollbars:** The [`PagedList`](/reference/androidx/paging/PagedList) provides the number of list items to the [`PagedListAdapter`](/reference/androidx/paging/PagedListAdapter). This information allows the adapter to draw a scrollbar that conveys the full size of the list. As new pages load, the scrollbar doesn't jump because your list doesn't change size.\n- **No loading spinner necessary:** Because the list size is already known, there's no need to alert users that more items are loading. The placeholders themselves convey that information.\n\nBefore adding support for placeholders, though, keep the following\npreconditions in mind:\n\n- **Requires a countable data set:** Instances of [`DataSource`](/reference/androidx/paging/DataSource) from the [Room\n persistence library](/topic/libraries/architecture/room) can efficiently count their items. If you're using a custom local storage solution or a [network-only\n data architecture](/topic/libraries/architecture/paging#network-only-data-arch), however, it might be expensive or even impossible to determine how many items comprise your data set.\n- **Requires adapter to account for unloaded items:** The adapter or presentation mechanism that you use to prepare the list for inflation needs to handle null list items. For example, when binding data to a [`ViewHolder`](/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder), you need to provide default values to represent unloaded data.\n- **Requires same-sized item views:** If list item sizes can change based on their content, such as social networking updates, crossfading between items doesn't look good. We strongly suggest disabling placeholders in this case.\n\nProvide feedback\n----------------\n\nShare your feedback and ideas with us through these resources:\n\n[Issue tracker](https://issuetracker.google.com/issues/new?component=413106&template=1096385) :bug:\n: Report issues so we can fix bugs.\n\nAdditional resources\n--------------------\n\nTo learn more about the Paging Library, consult the\nfollowing resources.\n\n### Samples\n\n- [Android Architecture Components Paging\n sample](https://github.com/android/architecture-components-samples/tree/paging2/PagingSample)\n- [Paging With Network Sample](https://github.com/android/architecture-components-samples/tree/paging2/PagingWithNetworkSample)\n\n### Codelabs\n\n- [Android Paging codelab](https://codelabs.developers.google.com/codelabs/android-paging/index.html?index=..%2F..%2Findex#0)\n\n### Videos\n\n- [Android Jetpack: manage infinite lists with RecyclerView and Paging\n (Google I/O '18)](https://www.youtube.com/watch?v=BE5bsyGGLf4)\n- [Android Jetpack: Paging](https://www.youtube.com/watch?v=QVMqCRs0BNA)\n\nRecommended for you\n-------------------\n\n- Note: link text is displayed when JavaScript is off\n- [Paging 2 library overview](/topic/libraries/architecture/paging)\n- [Migrate to Paging 3](/topic/libraries/architecture/paging/v3-migration)\n- [Gather paged data](/topic/libraries/architecture/paging/data)"]]