Android:Paging3:重复项目

Android: Paging3: Duplicates items

问题: 我在列表的开头得到 40 个项目,然后从 11 开始计数,在此之后,一切都很好。所以,1...40,11,12,13,...,300.

当我滚动到底部然后向上滚动以查看第一个项目时,项目已更改为 1,2,...,10,1,2,...,10,1 ,2,...,10,11,12,...,300.


但是,当我在 PagingConfig 中将 false 传递给 enablePlaceholders 时,当我滚动到底部时,我看到了我上面所说的问题(1,2,. .,40,11,...,300) 然后这 40 项突然消失了,我只看到 1,2,...,10 + 11,12,...,300(正确的方法);而且它不会改变或再次变得更糟。


ProductsPagingSource:

@Singleton
class ProductsPagingSource @Inject constructor(
    private val productsApi: ProductsApi
    //private val query: String
) : RxPagingSource<Int, RecyclerItem>() {

    override fun loadSingle(params: LoadParams<Int>): Single<LoadResult<Int, RecyclerItem>> {
        val position = params.key ?: STARTING_PAGE_INDEX
        //val apiQuery = query

        return productsApi.getBeersList(position, params.loadSize)
            .subscribeOn(Schedulers.io())
            .map { listBeerResponse ->
                listBeerResponse.map { beerResponse ->
                    beerResponse.toDomain()
                }
            }
            .map { toLoadResult(it, position) }
            .onErrorReturn { LoadResult.Error(it) }
    }

    private fun toLoadResult(
        @NonNull response: List<RecyclerItem>,
        position: Int
    ): LoadResult<Int, RecyclerItem> {
        return LoadResult.Page(
            data = response,
            prevKey = if (position == STARTING_PAGE_INDEX) null else position - 1,
            nextKey = if (response.isEmpty()) null else position + 1,
            itemsBefore = LoadResult.Page.COUNT_UNDEFINED,
            itemsAfter = LoadResult.Page.COUNT_UNDEFINED
        )
    }

}

ProductsListRepositoryImpl:

@Singleton
class ProductsListRepositoryImpl @Inject constructor(
    private val pagingSource: ProductsPagingSource
) : ProductsListRepository {

    override fun getBeers(ids: String): Flowable<PagingData<RecyclerItem>> = Pager(
        config = PagingConfig(
            pageSize = 10,
            enablePlaceholders = true,
            maxSize = 30,
            prefetchDistance = 5,
            initialLoadSize = 40
        ),
        pagingSourceFactory = { pagingSource }
    ).flowable

}

ProductsListViewModel:

class ProductsListViewModel @ViewModelInject constructor(
    private val getBeersUseCase: GetBeersUseCase
) : BaseViewModel() {

    private val _ldProductsList: MutableLiveData<PagingData<RecyclerItem>> = MutableLiveData()
    val ldProductsList: LiveData<PagingData<RecyclerItem>> = _ldProductsList

    init {
        loading(true)
        getProducts("")
    }

    private fun getProducts(ids: String) {
        loading(false)
        getBeersUseCase(GetBeersParams(ids = ids))
            .cachedIn(viewModelScope)
            .observeOn(AndroidSchedulers.mainThread())
            .subscribe {
                _ldProductsList.value = it
            }.addTo(compositeDisposable)
    }

}

ProductsListFragment:

@AndroidEntryPoint
class ProductsListFragment : Fragment(R.layout.fragment_product_list) {

    private val productsListViewModel: ProductsListViewModel by viewModels()

    private val productsListAdapter: ProductsListAdapter by lazy {
        ProductsListAdapter(::navigateToProductDetail)
    }

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

    private fun setupRecycler() {
        itemErrorContainer.gone()
        productListRecyclerView.adapter = productsListAdapter
    }

    private fun setupViewModel() {
        productsListViewModel.run {

            observe(ldProductsList, ::addProductsList)

            observe(ldLoading, ::loadingUI)

            observe(ldFailure, ::handleFailure)

        }
    }

    private fun addProductsList(productsList: PagingData<RecyclerItem>) {
        loadingUI(false)
        productListRecyclerView.visible()
        productsListAdapter.submitData(lifecycle, productsList)
    }
...

BASE_DIFF_CALLBACK:

val BASE_DIFF_CALLBACK = object : DiffUtil.ItemCallback<RecyclerItem>() {

    override fun areItemsTheSame(oldItem: RecyclerItem, newItem: RecyclerItem): Boolean {
        return oldItem.id == newItem.id
    }

    override fun areContentsTheSame(oldItem: RecyclerItem, newItem: RecyclerItem): Boolean {
        return oldItem == newItem
    }

}

BasePagedListAdapter:

abstract class BasePagedListAdapter(
    vararg types: Cell<RecyclerItem>,
    private val onItemClick: (RecyclerItem, ImageView) -> Unit
) : PagingDataAdapter<RecyclerItem, RecyclerView.ViewHolder>(BASE_DIFF_CALLBACK) {

    private val cellTypes: CellTypes<RecyclerItem> = CellTypes(*types)

    override fun getItemViewType(position: Int): Int {
        getItem(position).let {
            return cellTypes.of(it).type()
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return cellTypes.of(viewType).holder(parent)
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        getItem(position).let {
            cellTypes.of(it).bind(holder, it, onItemClick)
        }
    }

}

我必须为 pageSizeinitialLoadSize 设置相同的数字。此外,我必须将 enablePlaceholders 设置为 false

config = PagingConfig(
            pageSize = 10,
            enablePlaceholders = false,
            maxSize = 30,
            prefetchDistance = 5,
            initialLoadSize = 10
        ),

但是,我还是想知道这是否是正常的方式?如果是的话,我找不到任何指向这个的地方!如果不是,那是为什么?为什么 initialLoadSize 不能比 pageSize 有更多的价值?! 正如我们所见,initialLoadSize 的默认值为:

internal const val DEFAULT_INITIAL_PAGE_MULTIPLIER = 3
val initialLoadSize: Int = pageSize * DEFAULT_INITIAL_PAGE_MULTIPLIER,