标签: android-paging

Android PageKeyedDataSource(分页库)回调错误处理(src中有TODO异常)

我想通过loadInitialloadafter 中的callback.onError(或 callback.onRetryableError)将错误异常传递给我的分页适配器,但总是收到崩溃。经过一些调试后,我在库源中找到了这个: 在此处输入图片说明

这实际上是我在崩溃跟踪中收到的信息。只是“待办事项”。依赖版本:

'androidx.paging:paging-runtime-ktx:2.1.1'

在生产中出现这种异常消息是否可以,或者我做错了什么?无论如何......问题:任何其他“内置”方式将加载错误传递给适配器?

android android-architecture-components android-paging android-paging-library

6
推荐指数
0
解决办法
549
查看次数

如何在 Paging 3 库中检查列表大小或空列表

按照此处提供的说明,我已经能够成功实现新的 alpha07 版本的 Paging 3 库:https : //developer.android.com/topic/libraries/architecture/paging/v3-paged-data#g​​uava-livedata

但是,现在我需要检查返回的列表是否为空以便向用户显示视图或文本,但我无法在其分页设计的流结构中附加任何检查。

目前,这是我onViewCreated在遵循他们的指南后在 Java 中使用我的代码的方式:

        MyViewModel viewModel = new ViewModelProvider(this).get(MyViewModel.class);

        LifecycleOwner lifecycleOwner = getViewLifecycleOwner();
        Lifecycle lifecycle = lifecycleOwner.getLifecycle();
        Pager<Integer, MyEntity> pager = new Pager<>(new PagingConfig(10), () -> viewModel.getMyPagingSource());
        LiveData<PagingData<MyEntity>> pagingDataLiveData = PagingLiveData.cachedIn(PagingLiveData.getLiveData(pager), lifecycle);

        pagingDataLiveData.observe(lifecycleOwner, data -> adapter.submitData(lifecycle, data));
Run Code Online (Sandbox Code Playgroud)

我尝试.filter在我的datain上附加 a adapter.submitData(lifecycle, data),但null尽管列表为空,但它从未收到项目。

在这种情况下,如何检查提交给适配器的数据何时为空?我在他们的文档中找不到任何指针。

编辑:这是我找到的解决方案,在这里发布是因为所选的答案严格来说不是解决方案,也不是在 Java 中,而是引导我找到它的答案。

我有一个附加LoadStateListener我的转接器,监听时LoadTypeREFRESHLoadState的NotLoading,再检查是否adapter.getItemCount是0。

LoadType对于这种情况,可能采用不同的方法更合适,但到目前为止,刷新对我来说是有效的,所以我选择了那个。

示例:

// …
Run Code Online (Sandbox Code Playgroud)

android android-paging android-paging-library

6
推荐指数
1
解决办法
2217
查看次数

Android:LoadStateAdapter 不在 recyclerview gridlayout 中居中

我目前的问题是,LoadStateAdapter显示加载和错误状态的 my 不在 my 内居中recyclerview,它有一个 gridlayout 作为布局管理器。我在官方 android 开发者网站上没有找到任何关于此的信息,所以我在这里问:如何将我的LoadStateAdapter内部集中在我的 Recyclerview 中?

当前的

在此处输入图片说明

分段

@AndroidEntryPoint
class ShopFragment : Fragment(R.layout.fragment_shop), ShopAdapter.OnItemClickListener {
    private val shopViewModel: ShopViewModel by viewModels()
    private val shopBinding: FragmentShopBinding by viewBinding()
    @Inject lateinit var shopListAdapter: ShopAdapter

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        bindObjects()
    }

    private fun bindObjects() {
        shopBinding.adapter = shopListAdapter.withLoadStateFooter(ShopLoadAdapter(shopListAdapter::retry))
        shopListAdapter.clickHandler(this)
    }

    override fun onDestroyView() {
        requireView().findViewById<RecyclerView>(R.id.rv_shop).adapter = null
        super.onDestroyView()
    }
}
Run Code Online (Sandbox Code Playgroud)

适配器

@FragmentScoped
class ShopLoadAdapter(private val retry: () …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-recyclerview android-paging android-paging-3

6
推荐指数
2
解决办法
678
查看次数

将 pagingData 3 流与另一个流结合起来

我有一个 pagingData 流对象,我想将其与不同的融合位置流组合起来,以便它将根据 pagingdata 列表的每个项目进行相应的处理。

val point : Flow<Point> 
val pagingDate : Flow<PagingData>
Run Code Online (Sandbox Code Playgroud)

我尝试使用组合和组合变换,但它似乎不起作用,因为当更新应用程序崩溃时,并显示与 pagingData 3 相关的错误消息不能发出两次

java.lang.IllegalStateException: 尝试从pageEventFlow收集两次,这是非法操作。您是否忘记调用 Flow<PagingData<*>>.cachedIn(coroutineScope) ?

我可以选择哪些选项来使用流数据转换分页数据项?

android kotlin android-paging kotlin-coroutines kotlin-flow

6
推荐指数
1
解决办法
2822
查看次数

如何在 Android 中使用 Paging 3 库观察 Room 中更改的项目

我有一个正在使用 加载的数据列表PagingSource

这是我在 DAO 中的代码:

  @Query("SELECT * FROM Transport")
  fun getAllTransportsPaged(): PagingSource<Int, Transport>
Run Code Online (Sandbox Code Playgroud)

我正在通过以下方式获取分页数据Flow

Pager(config = PagingConfig(
          pageSize = 100,
          enablePlaceholders = true,
      )) {
        transportsDao.getAllTransportsPaged()
      }.flow
       .cachedIn(viewModelScope + Dispatchers.IO)
Run Code Online (Sandbox Code Playgroud)

我有另一个函数正在更新本地数据库中的项目:

@Query("UPDATE Transport SET favorite = :favorite WHERE id = :id")
suspend fun changeFavorite(id: Int, favorite: Boolean)
Run Code Online (Sandbox Code Playgroud)

调用此函数后,列表中的整个数据将从头开始加载。

有没有办法使用分页库仅获取更改的数据?

android android-room android-paging kotlin-coroutines android-paging-3

6
推荐指数
0
解决办法
1167
查看次数

如何实现基于游标的分页的 PagingSource.getRefreshKey - Android Jetpack Paging 3

我正在尝试使用Android Paging Library 3实现基于游标的分页(基于GraphQL Relay 规范),但我不知道如何实现。getRefreshKey

这是我尝试过的:

data class PagingKey(
    val before: String? = null,
    val after: String? = null,
)

class MoviePagingSource() : PagingSource<PagingKey, Movie>() {
    override suspend fun load(params: LoadParams<PagingKey>): LoadResult<PagingKey, Movie> {
        return try {
            val response = fetchMovies(
                before = params.key?.before,
                after = params.key?.after,
                pageSize = params.loadSize,
            )

            LoadResult.Page(
                data = response.edges.map { mapEdgeToMovie(it) },
                prevKey = if (response.pageInfo.hasPreviousPage) PagingKey(
                    before = response.pageInfo.startCursor
                ) else null,
                nextKey = if (response.pageInfo.hasNextPage) PagingKey(
                    after …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-jetpack android-paging android-paging-3

6
推荐指数
1
解决办法
2401
查看次数

将 Paging 3 alpha 更新到稳定会导致 Android 索引问题

嘿,我正在使用Paging 3库和ViewPager 2。它加载无限的数据。

\n
implementation "androidx.paging:paging-runtime-ktx:3.0.0-alpha07"\n
Run Code Online (Sandbox Code Playgroud)\n

数据源.kt

\n
package com.example.viewpagerexample\n\nimport java.util.*\n\nclass DataSource(\n    private val size: Int = 5,\n    private val currentDate: Date,\n    private val limitDate: Date?\n) {\n\n    fun returnData(pageNumber: Int): List<Date> {\n\n        val dateList = mutableListOf<Date>()\n        val startDateForPage = startDate(pageNumber)\n        val tempCalendar = Calendar.getInstance()\n\n        tempCalendar.time = startDateForPage\n        val lastDateForPage = endDate(startDateForPage)\n\n        while (tempCalendar.time < lastDateForPage) {\n            if (limitDate == null ||\n                tempCalendar.time.before(limitDate) ||\n                tempCalendar.time == limitDate\n            ) {\n                dateList.add(tempCalendar.time)\n                tempCalendar.add(Calendar.DATE, 1)\n            } else {\n                break\n …
Run Code Online (Sandbox Code Playgroud)

android kotlin android-paging android-paging-library android-paging-3

6
推荐指数
1
解决办法
2707
查看次数

Android Paging 3库PagingSource失效,导致列表由于刷新键错误(未使用空间)而跳转

由于我目前正在开发一个带有自定义数据库(不是 Room)的项目,因此我正在测试是否可以在该项目中使用 Paging 3 库。但是,我遇到了一个问题,如果您对数据进行更改并因此使分页源无效,则列表的重新创建会出现错误并跳转到不同的位置。发生这种情况是因为刷新键计算似乎是错误的,这很可能是由于初始加载加载了三页数据,但将其放入一页而引起的。

默认的分页源如下所示:

    override fun getRefreshKey(state: PagingState<Int, CustomData>): Int? {
        // Try to find the page key of the closest page to anchorPosition, from
        // either the prevKey or the nextKey, but you need to handle nullability
        // here:
        //  * prevKey == null -> anchorPage is the first page.
        //  * nextKey == null -> anchorPage is the last page.
        //  * both prevKey and nextKey null -> anchorPage is the initial page, so
        //    just …
Run Code Online (Sandbox Code Playgroud)

android android-paging android-paging-library android-paging-3

6
推荐指数
1
解决办法
7247
查看次数

如何将Android Paging Library与NetworkBoundResource集成

我的应用程序正在使用Android的Architecture组件库,并显示从具有无限滚动效果的分页REST api获取的项目列表。

我想做的是将Paging LibraryNetworkBoundResource结合使用,以便当用户向下滚动列表时,将从数据库中提取下一个项目并显示是否存在,并且同时调用API以更新数据库中的项目。

我找不到这两种模式同居的任何例子。

这是DAO:

@Query("SELECT * FROM items ORDER BY id DESC")
LivePagedListProvider<Integer,MyItem> loadListPaginated();
Run Code Online (Sandbox Code Playgroud)

这是我的NetworkBoundResource实现:

public class PagedListNetworkBoundResource extends NetworkBoundResource<PagedList<MyItem>, List<MyItem>> {

    @Override
    protected void saveCallResult(@NonNull List<MyItem> items) {
        // Inserting new items into DB
        dao.insertAll(items);
    }

    @Override
    protected boolean shouldFetch(@Nullable PagedList<MyItem> data) {
        return true;
    }

    @NonNull
    @Override
    protected LiveData<PagedList<MyItem>> loadFromDb() {
        return Transformations.switchMap(dao.loadListPaginated().create(INITIAL_LOAD_KEY, PAGE_SIZE),
                new Function<PagedList<MyItem>, LiveData<List<MyItem>>>() {

            @Override
            public LiveData<PagedList<MyItem>> apply(final PagedList<MyItem> input) {
                // Here I …
Run Code Online (Sandbox Code Playgroud)

paging android android-architecture-components android-paging

5
推荐指数
1
解决办法
2072
查看次数

分页库DataSource.Factory用于多个数据源

新的分页库使我们可以指定一个自定义数据源以用于数据分页。github上的分页库文档和示例代码向我们展示了如何通过创建DataSource.Factory的子类来创建自定义数据源实例,如下所示:

class ConcertTimeDataSourceFactory(private val concertStartTime: Date) :
    DataSource.Factory<Date, Concert>() {
    val sourceLiveData = MutableLiveData<ConcertTimeDataSource>()
    override fun create(): DataSource<Date, Concert> {
        val source = ConcertTimeDataSource(concertStartTime)
        sourceLiveData.postValue(source)
        return source
    }
}
Run Code Online (Sandbox Code Playgroud)

在真实的应用程序中,通常会有带有recyclerviews的多个视图,因此会有多个自定义数据源。因此,您最终会为每个数据源创建DataSource.Factory的多个实现,还是有一个更通用的解决方案?

android android-architecture-components android-jetpack android-paging

5
推荐指数
1
解决办法
2928
查看次数