0

分页 3 继续进行 api 调用而没有到达 recyclerview 的末尾我厌倦了将页面大小更改为 15,但使用基本分页源仍然会导致任何问题吗?这是视图的 XML

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout 
    android:id="@+id/stl"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.recyclerview.widget.RecyclerView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:clipToPadding="false"
            android:descendantFocusability="blocksDescendants"
            android:paddingStart="8dp"
            android:paddingTop="8dp"
            android:paddingEnd="8dp"
            android:paddingBottom="80dp" />

基本适配器

abstract class BasePagingAdapter<T : Any, VH : BasePagingAdapter.BaseViewHolder<T>>(diffCallback: DiffUtil.ItemCallback<T>) :
    PagingDataAdapter<T, VH>(diffCallback) {


    override fun onBindViewHolder(holder: VH, position: Int) {
        getItem(position).let { data -> holder.bindData(data!!) }
    }

    abstract class BaseViewHolder<T>(view: View) : RecyclerView.ViewHolder(view) {
        abstract fun bindData(data: T)
    }


    fun ViewGroup.inflateView(layoutRes: Int): View =
        LayoutInflater.from(this.context).inflate(layoutRes, this, false)
}

我的适配器

class OrdersPagingAdapter(val onCancelClick: (Order) -> Unit) :
    BasePagingAdapter<Order, BasePagingAdapter.BaseViewHolder<Order>>(
        OrdersPagingComparator
    ) {

    override fun onCreateViewHolder(
        parent: ViewGroup,
        viewType: Int
    ): BaseViewHolder<Order> =
        OrdersViewHolder(parent.inflateView(R.layout.item_order))

}

基本分页源

open class BasePagingSource<T : Any>(
    val call: suspend (Int) -> Response<BasePagingResponse<T>>
) :
    PagingSource<Int, T>() {

    override suspend fun load(
        params: LoadParams<Int>
    ): LoadResult<Int, T> {
        return try {
            val nextPageNumber = params.key ?: 1
            val response: Response<BasePagingResponse<T>> =
                call(nextPageNumber)
            if (response.code() == 200) {
                LoadResult.Page(
                    data = response.body()?.data!!,
                    prevKey = null,
                    nextKey = if (response.body()?.currentPage == response.body()?.totalPages) null
                    else
                        response.body()?.currentPage!! + 1
                )
            } else {
                LoadResult.Page(
                    data = emptyList(),
                    prevKey = null,
                    nextKey = null
                )
            }
        } catch (e: IOException) {
            return LoadResult.Error(e)
        } catch (e: HttpException) {
            return LoadResult.Error(e)
        }
    }

    override fun getRefreshKey(state: PagingState<Int, T>): Int? {
        return state.anchorPosition?.let { anchorPosition ->
            val anchorPage = state.closestPageToPosition(anchorPosition)
            anchorPage?.prevKey?.plus(1) ?: anchorPage?.nextKey?.minus(1)
        }
    }

}

我的存储库调用我尝试更改初始页面大小但仍然无法正常工作

fun getOrders() = Pager(
        config = PagingConfig(
            pageSize = 10,
            enablePlaceholders = false
        ), pagingSourceFactory = { OrdersPagingSource() }
    ).flow
4

0 回答 0