kotlin RecyclerView 分页 [英] kotlin RecyclerView Pagination

查看:42
本文介绍了kotlin RecyclerView 分页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要让我的 RecyclerView 仅加载 10 个项目并在滚动后加载更多 10 个项目并像这样工作.
我正在使用 Volley 添加数组中的项目.
这是我的 RecyclerView 适配器.

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.

推荐答案

尝试将此滚动侦听器与您的 recyclerview 结合使用.

Try using this scroll listener with your recyclerview.

在加载更多项目中放置您加载更多项目的逻辑.

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

isLastPage 将返回 true.

isLoading 获取数据时为真,获取数据时为假.

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()
}

将此添加到您的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)
}

现在在您的 recyclerview 适配器中添加以下方法.

Now in your recyclerview adapter add the following method.

这里的列表是在您的适配器中提供您的 recyclerview 的列表.
确保在 recyclerview 中初始化列表.

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)
}

这篇关于kotlin RecyclerView 分页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆