前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >教你简单实现RecyclerView自动滚动

教你简单实现RecyclerView自动滚动

作者头像
程思扬
发布2023-07-25 15:50:48
7430
发布2023-07-25 15:50:48
举报
文章被收录于专栏:程思阳的专栏程思阳的专栏

当RecyclerView内容过多,超出屏幕的时候,需要让它自己滚动展示数据,尤其是某些Android设备处于高处,或是不可被触摸点击的,这样的情况下,让其自己滚动展示数据尤为重要了 自动滚动的方案有很多种,目前比较常见又最简单的一种是:继承至RecyclerView,并实现runnable方法,每间隔10ms(delayTime)就去执行scrollby(x,y)方法,其中delayTime和x,y的值决定了滚动速度。

代码语言:javascript
复制
class AutoPollRecyclerView : RecyclerView {
    var autoPollTask //滚动线程
            : AutoPollTask? = null
    private var running //是否正在滚动
            = false
    private var canRun //是否可以自动滚动,根据数据是否超出屏幕来决定
            = false

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, @Nullable attrs: AttributeSet?) : super(context, attrs) {
        autoPollTask = AutoPollTask(this) //实例化滚动刷新线程
    }

    internal class AutoPollTask(reference: AutoPollRecyclerView?) : Runnable {
        private val mReference: WeakReference<AutoPollRecyclerView>
        override fun run() {
            val recyclerView: AutoPollRecyclerView = mReference.get() //获取recyclerview对象
            if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
                recyclerView.scrollBy(2, 2) //注意scrollBy和scrollTo的区别
                //延迟发送
                recyclerView.postDelayed(recyclerView.autoPollTask, delayTime)
            }
        }

        //使用弱引用持有外部类引用 防止内存泄漏
        init {
            mReference = WeakReference<AutoPollRecyclerView>(reference)
        }
    }

    //开启:如果正在运行,先停止->再开启
    fun start() {
        if (running) stop()
        canRun = true
        running = true
        postDelayed(autoPollTask, delayTime)
    }

    fun stop() {
        running = false
        removeCallbacks(autoPollTask)
    }

    companion object {
        private const val delayTime: Long = 50 //间隔多少时间后执行滚动
    }
}

上面代码实现了最基本的滚动功能,但有时候Adnroid设备可以触摸的话,而当前recyclerview正在滚动,又去滑动它,那就会造成界面错乱,数据错乱了,所以还需要重写拦截onTouchEvent方法,当触摸到recyclerview的时候,即在ACTION_DOWN的时,停止滚动线程,在ACTION_UP、ACTION_CANCEL时再开启线程。

代码语言:javascript
复制
fun onTouchEvent(e: MotionEvent): Boolean {
    when (e.action) {
        MotionEvent.ACTION_DOWN -> if (running) stop()
        MotionEvent.ACTION_UP, MotionEvent.ACTION_CANCEL, MotionEvent.ACTION_OUTSIDE -> if (canRun) start()
    }
    return super.onTouchEvent(e)
}

最后在设置itemCount的数量的时候不要返回集合的size,常见的返回Integer.MAX_VALUE,然后在获取数据的时候,用position和data.size()取余来获取实际的记录的索引值即可。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-07-24,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档