萌萌の初音
萌萌の初音
发布于 2024-07-28 / 150 阅读
0

RecyclerView搭配SeekBar实现滑动指示器功能

RecyclerView与SeekBar 联动代码(默认横向滚动)

    seekBar?.apply {
        setPadding(0)
        thumbOffset = 0
    }
    recyclerView.addOnScrollListener(object : OnScrollListener() {
        override fun onScrolled(recyclerView: RecyclerView, dx: Int, dy: Int) {
            super.onScrolled(recyclerView, dx, dy)
            val extent = recyclerView.computeHorizontalScrollExtent()
            val range = recyclerView.computeHorizontalScrollRange()
            val outside = range - extent
            val offset = recyclerView.computeHorizontalScrollOffset()
            seekBar?.setMax(outside)
            seekBar?.progress = offset
        }
    })

SeekBar的样式在xml里配置就不贴出来了