kotlin RecyclerView Pagination

|▌冷眼眸甩不掉的悲伤 提交于 2021-01-18 05:34:48

问题


I need to make my RecyclerView load only 10 items and load more 10 after scrolling and work like this.
I was add the items in array using Volley.
Here is my RecyclerView adapter.

class newsAdapter constructor(private val activety:MainActivity, private val ListOfCash:ArrayList<newsModling>,
                          val listener:BTNListener): RecyclerView.Adapter<newsAdapter.ViewHolder>(),BTNListener {

    override fun getItemCount(): Int = ListOfCash.size

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

    override fun onBindViewHolder(holder: ViewHolder, position: Int) {
        holder.bind(ListOfCash[position], listener, ListOfCash)
    }

    inner class ViewHolder(itemView: View?) : RecyclerView.ViewHolder(itemView) {
        fun bind(Data: newsModling, listener: BTNListener, listOfnew: ArrayList<newsModling>) {
            var ListOfnewsin = listOfnew[adapterPosition]

            var newstitle = ListOfnewsin.title
            var newsdate = ListOfnewsin.date

            itemView.newsDate.text = newsdate
            itemView.newsTitle.text = newstitle

            itemView.setOnClickListener{
                //var cashSTR = cashNumIn.toString()
            }
        }
    }
}

I don't know, what I must use or where I will type it.


回答1:


Try using this scroll listener with your recyclerview.

In load more items place your logic to load more items.

isLastPage will return true if there are no items to load.

isLoading will be true while you are fetching data and false when you have fetched the data.

import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView

/**
 * Pagination class to add more items to the list when reach the last item.
 */
abstract class PaginationScrollListener
/**
 * Supporting only LinearLayoutManager for now.
 *
 * @param layoutManager
 */
(var layoutManager: LinearLayoutManager) : RecyclerView.OnScrollListener() {

    abstract fun isLastPage(): Boolean

    abstract fun isLoading(): Boolean

    override fun onScrolled(recyclerView: RecyclerView?, dx: Int, dy: Int) {
        super.onScrolled(recyclerView, dx, dy)

        val visibleItemCount = layoutManager.childCount
        val totalItemCount = layoutManager.itemCount
        val firstVisibleItemPosition = layoutManager.findFirstVisibleItemPosition()

        if (!isLoading() && !isLastPage()) {
            if (visibleItemCount + firstVisibleItemPosition >= totalItemCount && firstVisibleItemPosition >= 0) {
                loadMoreItems()
            }//                    && totalItemCount >= ClothesFragment.itemsCount
        }
    }
    abstract fun loadMoreItems()
}

Add this to your recyclerview

var isLastPage: Boolean = false
var isLoading: Boolean = false

recyclerView?.addOnScrollListener(object : PaginationScrollListener(your_layoutManager) {
    override fun isLastPage(): Boolean {
        return isLastPage
    }

    override fun isLoading(): Boolean {
        return isLoading
    }

    override fun loadMoreItems() {
        isLoading = true
        //you have to call loadmore items to get more data 
        getMoreItems()
    }
})    

fun getMoreItems() {
    //after fetching your data assuming you have fetched list in your 
    // recyclerview adapter assuming your recyclerview adapter is 
    //rvAdapter
    after getting your data you have to assign false to isLoading 
    isLoading = false    

    rvAdapter.addData(list)
}

Now in your recyclerview adapter add the following method.

Here the list is the list which feeds your recyclerview in your Adapter.
Make sure you initialize the list in your recyclerview.

fun addData(listItems: ArrayList<yourObject>) {
    var size = this.listItems.size
    this.listItems.addAll(listItems)
    var sizeNew = this.listItems.size
    notifyItemRangeChanged(size, sizeNew)
}


来源:https://stackoverflow.com/questions/51433106/kotlin-recyclerview-pagination

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!