paging goes from use to abandonment, and then to use

Posted by alex_funky_dj on Mon, 21 Feb 2022 07:31:54 +0100

Remember that the paging framework should have been used in version 2.0.
But to be honest, if I want to realize paging Dehua, I must first write a lot of code and go around. No matter how encapsulated, the amount of code will not be small.
Layering is clear to the dog. However, at present, we are so busy that we can't use it out of the box. I really can't stick to it.
So, give up.
It is said that version 3.0 has been updated. At present, the latest version is 3.0.0-alpha 09.
After a look, there are still some changes. Then continue to use it.
Another BB sentence: writing a function is not easy to use. If you can use it in several functions and feel handy, it is easy to use!

1. Don't say anything. Do pagingsroute first
Suppose our paging data is in the form of pageNow and pagesize. Of course, your can also start and end.
So at this time, our code should be

class FencePagingSource : PagingSource<Int, PageBean<Geofence>>() {


    override suspend fun load(params: LoadParams<Int>): LoadResult<Int, PageBean<Geofence>> {
      //Don't ask what Int is. If you are happy, you can pass in a String and slowly turn it into Int~
      //And don't ask what Geofence is. Only wear the return amount entity class, do not write list < Geofence >
    }
}

Then write your own logic in load.

 override suspend fun load(params: LoadParams<Int>): LoadResult<Int, PageBean<Geofence>> {
        return try {

            val pageNow = params.key ?: 1
            val pageSize = params.loadSize
            val map = HashMap<String, Any>();
            map["pageNow"] = pageNow
            map["pageSize"] = pageSize
            val response = GeoFenceApi.create().getEctricFenceList(map);//Synchronous return results

            LoadResult.Page(
                data = response.data,
                prevKey = if (pageNow == 1) null else pageNow - 1,
                nextKey = if (response.isLastPage()) null else pageNow + 1
            )
        } catch (e: Exception) {
            LoadResult.Error(e)
        }
    }

2. Write Pager

//Flow<PagingData<Geofence>>
//pagingconfig is a configuration item that starts loading the next page according to the page size and distance. Same as previous version 2.0
val pager=Pager(PagingConfig(30, 3)){FencePagingSource()}.flow

3. Write adapter
Start with the simple. Suppose there is no header file.
Your own adapter must inherit PagingDataAdapter

//BaseViewHolder is self encapsulated
class FenceAdapter() : PagingDataAdapter<Geofence, BaseViewHolder<Geofence>>(
    diffCallback
) {
    companion object {
  
        val diffCallback = object : DiffUtil.ItemCallback<Geofence>() {
            override fun areItemsTheSame(oldItem: Geofence, newItem: Geofence): Boolean {

            }

            override fun areContentsTheSame(oldItem: Geofence, newItem: Geofence): Boolean {
    
            }

        }
    }

    override fun onBindViewHolder(holder: BaseViewHolder<Geofence>, position: Int) {
    //Binding data
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<Geofence> {
    //Create holder
    }
}

/** diffCallback Is to judge whether two item s are the same. Generally speaking, it is judged by primary key, that is, uniqueness
 Therefore, the above diffCallback can be written as**/
val diffCallback = object : DiffUtil.ItemCallback<Geofence>() {
            override fun areItemsTheSame(oldItem: Geofence, newItem: Geofence): Boolean {
                return newItem.id == oldItem.id
            }

            override fun areContentsTheSame(oldItem: Geofence, newItem: Geofence): Boolean {
                return newItem.id == oldItem.id
            }
        }

5. Write viewmodel
Define a livedata to subscribe to the foreground. At the same time, receive the pager just defined above
Suppose we have a warehouse class. repository.

 override fun queryFenceList(): Flow<PagingData<GeoFence>> {
        return Pager(PagingConfig(30, 3)){FencePagingSource()}.flow
    }

So the code in viewmodel is

val liveData=repository.queryFenceList().asLiveData()

6.activity subscription data and display UI

viewModel.liveData.observe(this) {
            lifecycleScope.launchWhenCreated {
                adapter.submitData(lifecycle, it)
            }
        }

Of course, don't be so naive. Remember to associate initialization of adapter with recycleView.

At this point, the preliminary use is over. The next article is about refreshing, retrying, loading and completing these disgusting things.



Author: Huang Xiaolou vincent
Link: https://www.jianshu.com/p/29c29cbfb831
Source: Jianshu
The copyright belongs to the author. For commercial reprint, please contact the author for authorization. For non-commercial reprint, please indicate the source.

Topics: eureka jetpack webview linq