代码之家  ›  专栏  ›  技术社区  ›  Alireza Ahmadi

永远不会调用PageList中的BoundaryCallback

  •  2
  • Alireza Ahmadi  · 技术社区  · 8 年前

    我在用 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
    }
    

    0 回复  |  直到 8 年前
    推荐文章