Android RecyclerView page/paging load,kotlin(3)

Android GridLayoutManager RecyclerView page load/paging load,by calculate screen position from already loaded item's position,kotlin(3)

package my.application

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.recyclerview.widget.GridLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.OnScrollListener

class MainActivity : AppCompatActivity() {
    companion object {
        val TAG = "fly"
    }

    val PAGE_SIZE = 50 //每一页的数据总数
    var PAGE_COUNT = 0 //记录已经加载的页数

    val mAdapter = MyAdapter()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val recyclerView = findViewById<RecyclerView>(R.id.recycler_view)
        recyclerView.layoutManager = GridLayoutManager(this, 4).apply {
            orientation = GridLayoutManager.VERTICAL
        }

        //假设已经知道全量数据,后续的分段是把源数据一片一片的截出来,装到新的分页数据集里面
        val ITEMS = ArrayList<MyData>()
        for (i in 0..1000) {
            var data = MyData("$i", "@$i")
            ITEMS.add(data)
        }

        recyclerView.adapter = mAdapter

        var mIsLoading = false
        var rang = IntArray(2) { 0 }
        recyclerView.addOnScrollListener(object : OnScrollListener() {
            override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
                if (mIsLoading) {
                    Log.d(TAG, "正在加载!")
                    return
                }

                getOutRange(recyclerView, rang)

                var pos = ITEMS.size - PAGE_SIZE * PAGE_COUNT
                Log.d(TAG, "onScrolled ${rang[0]}->${rang[1]} $pos")
                if (rang[0] - pos < PAGE_SIZE) {
                    mIsLoading = true

                    PAGE_COUNT++

                    Log.d(TAG, "load $PAGE_COUNT ->")
                    paging(ITEMS, PAGE_SIZE, PAGE_COUNT)
                    Log.d(TAG, "load $PAGE_COUNT <-")

                    mIsLoading = false
                }
            }
        })

        PAGE_COUNT = 1
        paging(ITEMS, PAGE_SIZE, PAGE_COUNT)

        //recyclerView启动后默认滚到最底部
        recyclerView.scrollToPosition(mAdapter.getItemCount() - 1)
    }

    fun paging(
        sourceItems: ArrayList<MyData>,
        pageSize: Int,
        pageCount: Int
    ) {
        Log.d(TAG, "加载第 $pageCount 页")

        //新的分页数据集
        var pageItems: ArrayList<MyData>? = ArrayList()

        var count = pageCount * pageSize
        if (count > sourceItems.size) {
            //因为分页是基于整数PAGE_SIZE分页的,很可能PAGE_SIZE * PAGE_COUNT大于实际sourceItems长度
            pageItems = sourceItems.toList() as ArrayList<MyData>
        } else {
            var pos = sourceItems.size - pageSize * pageCount
            for (i in 0 until pos) {
                //上部分加入占位数据
                pageItems?.add(MyData("-1", "-1"))
            }

            for (i in 0 until count) {
                //截取真实的源数据到新的分页数据items里面
                pageItems?.add(sourceItems.get(pos + i)!!)
            }
        }

        mAdapter.onChange(pageItems!!)
    }

    fun getOutRange(rv: RecyclerView, outRange: IntArray) {
        outRange[0] = (rv.layoutManager as GridLayoutManager?)!!.findFirstVisibleItemPosition()
        outRange[1] = (rv.layoutManager as GridLayoutManager?)!!.findLastVisibleItemPosition()
    }
}

<?xml version="1.0" encoding="utf-8"?>
<androidx.recyclerview.widget.RecyclerView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/recycler_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" />

package my.application

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

class MyAdapter() : RecyclerView.Adapter<MyViewHolder>() {
    var items: ArrayList<MyData>? = ArrayList()

    fun onChange(items: ArrayList<MyData>) {
        this.items?.clear()

        this.items = items

        this.notifyDataSetChanged()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(android.R.layout.simple_list_item_2, parent, false)
        return MyViewHolder(view)
    }

    override fun getItemCount(): Int {
        return items?.size!!
    }

    override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
        holder.name.text = items?.get(position)?.name
        holder.desc.text = items?.get(position)?.desc
    }
}

class MyViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
    val name: TextView = itemView.findViewById(android.R.id.text1)
    val desc: TextView = itemView.findViewById(android.R.id.text2)
}

data class MyData(var name: String, var desc: String)

Android Paging3/Page3 Cursor query segment photo into 3 grid RecyclerView,kotlin (2)_zhangphil的博客-CSDN博客基于Android官方Paging Library的RecyclerView分页加载框架我之前写了一篇RecyclerView分页加载机制的文章,是基于Android官方的AsyncListUtil实现的,详情见附录文章1。Android Paging Library是Android官方support。基于Android官方Paging Library的RecyclerView分页加载框架_android setmainthreadexecutor_zhangphil的博客-CSDN博客。https://blog.csdn.net/zhangphil/article/details/130751993

Android Paging 3 Flow or LiveData,kotlin(1)_zhangphil的博客-CSDN博客【代码】Android Paging 3,kotlin(1)https://blog.csdn.net/zhangphil/article/details/130735753

基于Android官方Paging Library的RecyclerView分页加载框架_android setmainthreadexecutor_zhangphil的博客-CSDN博客基于Android官方Paging Library的RecyclerView分页加载框架我之前写了一篇RecyclerView分页加载机制的文章,是基于Android官方的AsyncListUtil实现的,详情见附录文章1。现在再介绍一种RecyclerView分页加载框架:Android Paging Library。Android Paging Library是Android官方supporthttps://blog.csdn.net/zhangphil/article/details/78627332

基于Android官方AsyncListUtil优化改进RecyclerView分页加载机制(一)_recyclerview 分页机制_zhangphil的博客-CSDN博客基于Android官方AsyncListUtil优化改进RecyclerView分页加载机制(一)Android AsyncListUtil是Android官方提供的专为列表这样的数据更新加载提供的异步加载组件。基于AsyncListUtil组件,可以轻易实现常见的RecyclerView分页加载技术。AsyncListUtil技术涉及的细节比较繁复,因此我将分别写若干篇文章,分点、分解Asynchttps://blog.csdn.net/zhangphil/article/details/78603499

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

zhangphil

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值