gpt4 book ai didi

Android:Paging3:重复项

转载 作者:行者123 更新时间:2023-12-05 00:12:03 32 4
gpt4 key购买 nike

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

enter image description here

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


但是,当我将 false 传递给 PagingConfig 中的 enablePlaceholders 时,当我滚动到底部时,我看到了我所说的问题above(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,

关于Android:Paging3:重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63439472/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com