美文网首页
RecyclerView滑动到指定位置,并使指定位置位于列表最上

RecyclerView滑动到指定位置,并使指定位置位于列表最上

作者: 初见soulmate | 来源:发表于2021-06-09 09:20 被阅读0次

import androidx.recyclerview.widget.LinearSmoothScroller
import androidx.recyclerview.widget.RecyclerView

/**

  • 滑动到指定位置,并使指定位置位于列表最上面
    */
    fun RecyclerView.scrollItemToTop(position: Int) {
    val smoothScroller: LinearSmoothScroller = object : LinearSmoothScroller(context) {
    override fun getVerticalSnapPreference(): Int {
    return SNAP_TO_START
    }
    }
    smoothScroller.targetPosition = position
    layoutManager?.startSmoothScroll(smoothScroller)
    }

相关文章

网友评论

      本文标题:RecyclerView滑动到指定位置,并使指定位置位于列表最上

      本文链接:https://www.haomeiwen.com/subject/adnseltx.html