Android Jetpack Paging 3 下拉刷新和加载更多代码示例

使用Paging3实现列表的下拉刷新和加载更多


首先定义列表布局文件

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

            <androidx.recyclerview.widget.RecyclerView
                android:id="@+id/recycler_view"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"/>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

定义PagingSource

class RecordPagingSource : PagingSource<Int,RecordItem>() {

    private val lotteryRepository = LotteryRepository()

    override fun getRefreshKey(state: PagingState<Int, RecordItem>): Int? {
        return null
    }

    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, RecordItem> {

        return try {
            val page = params.key ?: 1
            val pageSize = params.loadSize

            var list = listOf<RecordItem>()
            var result: ServiceResult<RecordItem> = repository.recordList(page,pageSize)

            delay(1000)
            //模拟数据
            if(page  <  5){
                for(index in (page-1)*pageSize .. page * pageSize){
                    result.data.items.add(RecordItem("2021-11-11 11:11:11",1, "名字$index"))
                }
            }

            if(result.errcode.toInt() == 0){
                list = result.data.items
            }

            var prevKey: Int?
            var nextKey: Int?

            var initLoadSize = 16
            if(page == 1){
                prevKey = null
                nextKey = initLoadSize/pageSize  + 1
            }else{
                prevKey = page - 1
                nextKey = if (result.data.items.size < pageSize) null else page + 1
            }

            return  LoadResult.Page(
                data = list,
                prevKey = prevKey,
                nextKey = nextKey

            )
        } catch (e: Exception) {
            e.printStackTrace()
            LoadResult.Error(e)
        }

    }
}

定义ViewModel

class RecordViewModel : ViewModel() {

    fun loadRecordList() : Flow<PagingData<RecordItem>> {
        return Pager(
            config = PagingConfig(
                pageSize = 8,
                prefetchDistance = 1,
                initialLoadSize = 16
            ),
            pagingSourceFactory = {
                RecordPagingSource()
            }
        ).flow
    }
}

定义ViewModelFactory

class RecordViewModelFactory : ViewModelProvider.NewInstanceFactory() {
    @Suppress("UNCHECKED_CAST")
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return RecordViewModel() as T
    }
}

定义PagingDataAdapter

class RecordPagingDataAdapter(
    val activity: RecordActivity
) :PagingDataAdapter<RecordItem, RecordPagingDataAdapter.ViewHolder>(

    object:DiffUtil.ItemCallback<RecordItem>(){

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

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

{

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        val item = getItem(position)
        item?.let {
            holder.nameText.text = item.name
            with(holder.itemView) {
                tag = item
            }
        }

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder {
        val binding = ItemListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ViewHolder(binding)
    }

    inner class ViewHolder(binding: ItemListBinding) : RecyclerView.ViewHolder(binding.root) {
        val nameText: TextView = binding.nameText
    }

}

在Activity中

//view model
recordViewModel = ViewModelProvider(this, RecordViewModelFactory()).get(RecordViewModel::class.java)

        // Adapter
        //var recyclerViewAdapter = RecordRecyclerViewAdapter(activity as RecordActivity,coinDataList)
        //lotteryRecordRecyclerView.adapter = recyclerViewAdapter

        var recyclerViewAdapter = RecordPagingDataAdapter(activity as RecordActivity)

        binding.apply {

            recyclerView.adapter = recyclerViewAdapter.withLoadStateFooter(
                LoadMoreAdapter(activity as RecordActivity)
            )
            recyclerView.layoutManager = LinearLayoutManager(activity)

            var loading = false
            // 刷新
            binding.swipeRefreshLayout.setOnRefreshListener {
                recyclerViewAdapter.refresh()
                /*lifecycleScope.launch{
                    recyclerViewAdapter.loadStateFlow.collectLatest { state->
                        logI(TAG,"setOnRefreshListener:" + state.refresh.toString())
                        if (state.refresh !is LoadState.Loading) recyclerViewAdapter.refresh()
                    }
                }*/
            }

            recyclerViewAdapter.addLoadStateListener {
                logI(TAG,"addLoadStateListener:" + it.refresh.toString())

                when (it.refresh) {
                    is LoadState.NotLoading -> {
                        loading = false
                        binding.swipeRefreshLayout.isRefreshing = false
                    }
                    is LoadState.Loading -> {
                        loading = true
                    }
                    is LoadState.Error -> {
                        loading = false
                        binding.swipeRefreshLayout.isRefreshing = false
                    }
                }
            }

        }

        // 加载数据
        lifecycleScope.launchWhenCreated {
            recordViewModel.loadRecordList().collectLatest {
                recyclerViewAdapter.submitData(it)
            }
        }

以上


内容来源与作者发布和网络,如有版权相关问题请及时与我们取得联系,我们将立即删除。

关于作者

还如一梦中 点击这里给我发消息

相关推荐

这些你应该也会喜欢

Android Swicth按钮样式自定义

Switch android:id="@+id/switch_btn" android:layout_width="wrap_content" android:layout_height=...

2022-04-14 10:30:56 还如一梦中
Pagging3写起来太麻烦,简单封装及其简单

Pagging3分页写起来很麻烦,这里分享一下我的简化开发的方法,思路就是把获取数据的函数分离出来首先定义一个基础的PagingSource接收任意类型的数据条目类型,可以看出这里将获取key的函数都...

2022-04-13 12:34:12 还如一梦中
在项目中使用Hilt Retrofit使用总结

直接开始,首先我们看看怎么使用Hilt编写Retrofit接口请求类用@Provides注解定义可注入的实例的提供者函数,如:provideHttpClient()用于提供OkHttpClient的实...

2021-12-24 09:53:05 还如一梦中
Android Jetpack Paging 3 下拉刷新和加载更多代码示例

使用Paging3实现列表的下拉刷新和加载更多首先定义列表布局文件androidx.swiperefreshlayout.widget.SwipeRefreshLayout a...

2021-12-16 09:43:09 还如一梦中
StatefulBuilder实现Dialog的刷新

在Flutter中使用Dialog时,因为showDialog返回的context与当前页面的context不是同一个,所以通过setState是无法刷新Dialog里面的页面的。这时我们就可以用St...

2021-06-21 17:08:14 还如一梦中
2021Flutter发布package到Pub终极解决方案

整整折腾了1天半终于成功吧package上传到了pub上。先花了15在淘宝找人代注册了个google账户。先取消配置的环境变量FLUTTER_STORAGE_BASE_URL https://stor...

2021-05-27 11:49:52 还如一梦中
Retrofit+Gson解析工具类封装

定义Retrofit Service类import android.os.Build import cn.wtyc.weiwogroup.global.Constant import cn.wtyc....

2021-12-16 10:38:34 还如一梦中
PHP调用Shell脚本,命令找不到环境变量问题解决

PHP调用Shell脚本需要设置php执行shell的root权限,否则执行sh会找不到命令。1.通过system函数获取当前apache的用户,代码如下:?php header("Content-...

2022-01-26 11:17:01 还如一梦中
Spleeter歌曲分离环境安装笔记

#####################【Windows 安装 spleeter】########################### # 环境变量PATH中 E:\project\tools\p...

2022-01-25 17:32:19 还如一梦中
升级gradle7.0.2错误a repository over an insecure HTTP connection

Specifies whether it is acceptable to communicate with a repository over an insecure HTTP connection...

2021-12-21 09:25:24 还如一梦中

友情链接