我需要使RecyclerView
仅加载10个项目,并在滚动并像这样工作后再加载10个项目。
我是使用Volley
将项目添加到数组中的。
这是我的RecyclerView
适配器。
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()
}
}
}
}
我不知道我必须使用什么或在哪里键入
。答案 0 :(得分:3)
尝试将此滚动侦听器与recyclerview
一起使用。
在加载更多项目时,放置逻辑以加载更多项目。
如果没有要加载的项目, isLastPage
将返回true。
isLoading
在获取数据时为true,在获取数据时为false。
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
适配器中添加以下方法。
此列表是在您的适配器中提供recyclerview
的列表。
确保在recyclerview
中初始化列表。
fun addData(listItems: ArrayList<yourObject>) {
var size = this.listItems.size
this.listItems.addAll(listItems)
var sizeNew = this.listItems.size
notifyItemRangeChanged(size, sizeNew)
}