android - 在到达最后一张卡片之前实现分页数据加载的问题
问题描述
我正在尝试在我的项目中实现分页,但我不知道它如何从Firestore加载所有数据。
我尝试了自己的分页方法,但我不知道分页有什么问题。
代码 :
赋值变量
lateinit var adapter: BooksListAdapter
var booksArray = arrayListOf<Book>()
var totalItemCount: Int = 0
var lastVisibleItem : Int = 0
var visibleThreshold : Int = 0
var item_count = 6
private var loading : Boolean = false
布局管理器和适配器声明
val mLayoutManager = LinearLayoutManager(this)
books_list.setLayoutManager(mLayoutManager)
adapter = BooksListAdapter(arrayListOf(), singlePage, multiplePage, this)
books_list.adapter = adapter
第一个视图中的数据加载
private fun getBooksData() {
bookCollection
.orderBy("created_at", Query.Direction.DESCENDING)
.limit(item_count.toLong()).get()
.addOnSuccessListener { booksSnapshot ->
if (!booksSnapshot.isEmpty) {
for (bookSnapShot in booksSnapshot.documents) {
val hashmap = bookSnapShot.data
hashmap?.put("id", bookSnapShot.id)
val bookData = Gson().toJson(hashmap)
val book = Gson().fromJson<Book>(bookData, Book::class.java)
booksArray.add(book)
}
shimmer_view_container_book.stopShimmerAnimation()
shimmer_view_container_book.setVisibility(View.GONE)
rcv_book.setVisibility(View.VISIBLE)
adapter.booksList.addAll(booksArray)
adapter.notifyDataSetChanged()
}
}
.addOnFailureListener { exception ->
Toast.makeText(this, exception.message, Toast.LENGTH_SHORT).show()
}
}
和我的 onaddScrollListener :
books_list.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView,
dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
visibleThreshold = books_list.getChildCount();
totalItemCount = mLayoutManager.getItemCount()
lastVisibleItem = mLayoutManager
.findLastVisibleItemPosition()
if (!loading && totalItemCount <= lastVisibleItem + visibleThreshold) {
getMoreBooks(adapter.getLastBook())
loading = true
}
}
})
获取更多书籍:
private fun getMoreBooks(lastBook: Timestamp) {
bookCollection
.orderBy("created_at", Query.Direction.DESCENDING)
.startAfter(lastBook)
.limit(item_count.toLong()).get()
.addOnSuccessListener {booksSnapshot ->
if (!booksSnapshot.isEmpty) {
for (bookSnapShot in booksSnapshot.documents) {
val hashmap = bookSnapShot.data
hashmap?.put("id", bookSnapShot.id)
val bookData = Gson().toJson(hashmap)
val book = Gson().fromJson<Book>(bookData, Book::class.java)
adapter.addDatainList(book)
}
}
}
}
适配器 :
class BooksListAdapter (var booksList: ArrayList<Book>, val singlePage: View.OnClickListener, val multiplePage: View.OnClickListener, val context : Context): RecyclerView.Adapter<BooksListAdapter.ViewHolder>() {
var bookid = ""
val storageReference = FirebaseStorage.getInstance()
fun getLastBook(): Timestamp{
return booksList.get(booksList.size-1).created_at
}
fun addDatainList(book : Book){
booksList.add(book)
notifyDataSetChanged()
}
override fun onCreateViewHolder(parent: ViewGroup, position: Int): ViewHolder {
val itemView = LayoutInflater.from(parent.context).inflate(R.layout.single_book, parent, false)
return ViewHolder(itemView)
}
override fun getItemCount(): Int {
return booksList.size
}
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
if (booksList.get(position).picture != "null"){
GlideApp.with(context).load(booksList[position].picture?.let { storageReference.reference.child(it) }).centerCrop().override(300, 300).into(holder.bookImage!!)
}else{
GlideApp.with(context).load(R.drawable.picture).centerCrop().override(300, 300).into(holder.bookImage!!)
}
holder.bookCard!!.setOnClickListener {
if (booksList[position].pages > 1){
bookid = booksList.get(position).id
multiplePage.onClick(it)
}else{
bookid = booksList.get(position).id
singlePage.onClick(it)
}
}
holder.bookTitle!!.setText(booksList[position].book_name)
holder.bookPages!!.setText("Book Pages : ${booksList[position].pages}")
holder.bookAuther!!.setText(booksList[position].auther)
holder.bookDescription!!.setText(booksList[position].book_description)
}
class ViewHolder (itemView: View) : RecyclerView.ViewHolder(itemView) {
var bookCard: CardView? = null
var bookTitle: TextView? = null
var bookDescription: TextView? = null
var bookPages: TextView? = null
var bookAuther: TextView? = null
var bookImage : ImageView? = null
init {
this.bookCard = itemView.findViewById(R.id.single_book_card)
this.bookTitle = itemView.findViewById(R.id.book_title_txt)
this.bookDescription = itemView.findViewById(R.id.book_desc_txt)
this.bookAuther = itemView.findViewById(R.id.auther_txt)
this.bookPages = itemView.findViewById(R.id.book_pages)
this.bookImage = itemView.findViewById(R.id.book_img)
}
}
}
先感谢您
如果你想生病提供其他信息
解决方案
我的例子希望它有帮助
page = 0
var previous = 0
val viewth = 20
recyclerView.addOnScrollListener(object : RecyclerView.OnScrollListener() {
override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
super.onScrolled(recyclerView, dx, dy)
visible = gridLayoutManager.childCount
total = gridLayoutManager.itemCount
past = gridLayoutManager.findFirstVisibleItemPosition()
if (dy > 0) {
if (loading) {
if (total > previous) {
loading = false
previous = total
}
}
if (!loading && (total - visible) <= (past + viewth)) {
page++
getNextData(page.toString(), range)//call your next set
loading = true
}
} else {
mSwipeRefreshLayout.isEnabled = gridLayoutManager.findFirstCompletelyVisibleItemPosition() == 0
}
}
override fun onScrollStateChanged(recyclerView: RecyclerView, newState: Int) {
super.onScrollStateChanged(recyclerView, newState)
mSwipeRefreshLayout.isEnabled = gridLayoutManager.findFirstCompletelyVisibleItemPosition() == 0
}
})
推荐阅读
- flutter - 如何在飞镖中启用断言?
- unit-testing - 如何在 XQuery 脚本中启动 BaseX 单元测试模块?
- javascript - 如何在 JavaScript 函数中进行函数输入?
- python - Python:如何拆分目录中的csv文件的名称
- java - 尝试递归打印给定目录中的所有文件路径时出错
- python - Tkinter.tclerror:没有显示名称和没有 $display 环境变量 Ubuntu 20.04
- javascript - 如何按数组中的键嵌套分组并在javascript中计算sum、avg?
- pytorch - TypeError: reshape(): argument 'input' (position 1) must be Tensor, not numpy.ndarray
- php - Guzzle - 外部文件“直通”
- bayesian - brms hurdle_lognormal 模型:这是将后验值转换为 y 变量单位并将障碍值转换为概率的正确方法吗?