Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kotlin RecyclerView Pagination

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.

like image 451
Nasser El Arab Avatar asked Jul 19 '18 23:07

Nasser El Arab


1 Answers

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)
}
like image 166
Rohit Sharma Avatar answered Sep 27 '22 22:09

Rohit Sharma