当前位置:   article > 正文

RecycleView横向来回自动滚动

RecycleView横向来回自动滚动

以下做出两处改动
1.添加每次滚动距离设置
2.实现来回滚动
3.代码使用kotlin

import android.content.Context
import android.os.Build
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.animation.LinearInterpolator
import androidx.annotation.RequiresApi
import androidx.recyclerview.widget.RecyclerView
import java.lang.ref.WeakReference

class AutoPollRecyclerView(context: Context, attrs: AttributeSet?) : RecyclerView(context, attrs) {
    internal var autoPollTask: AutoPollTask
    private var running: Boolean = false //标示是否正在自动轮询
    private var canRun: Boolean = false//标示是否可以自动轮询,可在不需要的是否置false
    //改动1:添加每次滚动距离设置
    var distanceOnce = 550
        set(value) {
            autoPollTask = AutoPollTask(this, value)
        }

    init {
        autoPollTask = AutoPollTask(this, distanceOnce)
    }

    internal class AutoPollTask//使用弱引用持有外部类引用->防止内存泄漏
    (reference: AutoPollRecyclerView, private val distanceOnce: Int) : Runnable {
        private val mReference: WeakReference<AutoPollRecyclerView> = WeakReference(reference)
        private var isScrollToEnd = false

        @RequiresApi(Build.VERSION_CODES.M)
        override fun run() {
            val recyclerView = mReference.get()
            if (recyclerView != null && recyclerView.running && recyclerView.canRun) {
        		//改动2:实现来回滚动
                if (recyclerView.computeHorizontalScrollExtent() + recyclerView.computeHorizontalScrollOffset() >= recyclerView.computeHorizontalScrollRange()) {
                    isScrollToEnd = true
                } else if (recyclerView.computeHorizontalScrollOffset() == 0) {
                    isScrollToEnd = false
                }
                recyclerView.smoothScrollBy(if (isScrollToEnd) {
                    -distanceOnce
                } else {
                    distanceOnce
                }, 0, LinearInterpolator())
                recyclerView.postDelayed(recyclerView.autoPollTask, TIME_AUTO_POLL)
            }
        }
    }

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

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

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

    companion object {
        private val TIME_AUTO_POLL: Long = 2000
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76

感谢
参考文档:https://blog.csdn.net/merbn/article/details/97146937

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/312664
推荐阅读
相关标签
  

闽ICP备14008679号