PageList中的BoundaryCallback永远不会被调用

时间:2018-07-15 07:42:00

标签: android android-architecture-components android-paging

我在Paging Library中使用PagedListAdapter,在BoundaryCallback中使用PagedList来知道用户何时到达列表末尾。问题是我的回调中的方法永远不会被调用。

这是我的数据源代码:

class PropertyDataSource : ItemKeyedDataSource<Int, Property>() {

    override fun loadInitial(params: LoadInitialParams<Int>, callback: LoadInitialCallback<Property>) {
        callback.onResult(getProperties(1, params.requestedLoadSize))
    }

    override fun loadAfter(params: LoadParams<Int>, callback: LoadCallback<Property>) {
        Thread.sleep(1000)
        callback.onResult(getProperties(params.key + 1, params.requestedLoadSize + params.key))
    }

    override fun loadBefore(params: LoadParams<Int>, callback: LoadCallback<Property>) {

    }

    override fun getKey(item: Property): Int {
        return item.id
    }
}

我的边界回调代码是

class MyBoundryCallBack: PagedList.BoundaryCallback<Property>() {
    override fun onItemAtEndLoaded(itemAtEnd: Property) {
        Log.e("alz", "at end $itemAtEnd")
    }

    override fun onItemAtFrontLoaded(itemAtFront: Property) {
        Log.e("alz", "at front $itemAtFront")
    }

    override fun onZeroItemsLoaded() {
        Log.e("alz", "zero item loaded")
    }
}

这是我的活动代码

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)

    val adapter = MyListAdapter(PropertyDiffCallback())

    mainRecyclerView.adapter = adapter
    mainRecyclerView.layoutManager = LinearLayoutManager(this)

    val propertyDataSourceFactory = PropertyDataSourceFactory()

    val config = PagedList.Config.Builder()
            .setPageSize(2)
            .setInitialLoadSizeHint(3)
            .setEnablePlaceholders(false)
            .build()

    val pagedList = LivePagedListBuilder(propertyDataSourceFactory, config)
            .setBoundaryCallback(MyBoundryCallBack())
            .build()

    pagedList.observe(this, Observer {
        adapter.submitList(it)
    }
}

我的适配器代码如下所示:

class MyListAdapter(diffCallback: DiffUtil.ItemCallback<Property>) : 
    PagedListAdapter<Property, RecyclerView.ViewHolder>(diffCallback) {

    class ItemHolder(itemView: View) : RecyclerView.ViewHolder(itemView){
        val titleTV: TextView = itemView.findViewById(R.id.title)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return ItemHolder(LayoutInflater.from(parent.context).inflate(R.layout.item_property, parent, false))
    }

    override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
        if(getItem(position) == null) return

        val itemHolder = holder as ItemHolder
        itemHolder.titleTV.text = getItem(position)!!.title
    }
}

这是我对getProperties()的实现,它将返回一些模拟数据。稍后,我将对其进行更改以从服务器加载数据。

fun getProperties(from: Int, to: Int): List<Property> {
    val list = ArrayList<Property>()
    for (i in from..to){
        list.add(Property(i, "item $i"))
    }
    return list
}

请注意,我的代码工作正常,并且适配器在结束时请求更多数据,当PagedList结束时,我只是没有得到回调。

0 个答案:

没有答案
相关问题