修复嵌套recyclerview android中滚动条的高度

时间:2019-07-11 06:20:55

标签: android android-recyclerview scrollbar

我在另一个水平Recyclerview中有一个水平Recyclerview。现在,我要显示scrollbar。但是,随着我滚动,滚动条的高度正在改变。滚动时,我希望滚动条的高度固定。我该如何实现?我有一个自定义LinearLayoutManager。但是它不能正常工作。有更好的解决方案吗?

class CoolLayoutManager(context: Context,orientation: Int,
  reverseLayout: Boolean) : LinearLayoutManager(context,orientation,
    reverseLayout) {

  init {
    isSmoothScrollbarEnabled = true
  }

  override fun computeHorizontalScrollExtent(state: State): Int {
    val count = childCount
    return if (count > 0) {
      SMOOTH_VALUE * 3
    } else 0
  }

  override fun computeHorizontalScrollRange(state: State): Int {
    return Math.max((itemCount - 1) * SMOOTH_VALUE, 0)
  }

  override fun computeHorizontalScrollOffset(state: State): Int {
    val count = childCount

    if (count <= 0) {
      return 0
    }
    if (findLastCompletelyVisibleItemPosition() == itemCount - 1) {
      return Math.max((itemCount - 1) * SMOOTH_VALUE, 0)
    }

    val widthOfScreen: Int
    val firstPos = findFirstVisibleItemPosition()
    if (firstPos == RecyclerView.NO_POSITION) {
      return 0
    }
    val view = findViewByPosition(firstPos) ?: return 0
// Top of the view in pixels
    val top = getDecoratedTop(view)
    val width = getDecoratedMeasuredWidth(view)
    widthOfScreen = if (width <= 0) {
      0
    } else {
      Math.abs(SMOOTH_VALUE * top / width)
    }
    return if (widthOfScreen == 0 && firstPos > 0) {
      SMOOTH_VALUE * firstPos - 1
    } else SMOOTH_VALUE * firstPos + widthOfScreen
  }


  companion object {
    private const val SMOOTH_VALUE = 100
  }
}

这是布局的屏幕截图: enter image description here

0 个答案:

没有答案