How to disable overscroll effect on RecyclerView ONLY when you can't scroll anyway ?

前端 未结 5 1054
囚心锁ツ
囚心锁ツ 2021-02-07 00:38

Background

Sometimes, all items of the recyclerView are already visible to the user.

In this case, it wouldn\'t matter to the user to see overscroll effect, be

5条回答
  •  抹茶落季
    2021-02-07 01:20

    Since android:overScrollMode="ifContentScrolls" is not working for RecyclerView(see https://issuetracker.google.com/issues/37076456) I found some kind of a workaround which want to share with you:

    class MyRecyclerView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
    ) : RecyclerView(context, attrs, defStyleAttr) {
    
        override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
            super.onLayout(changed, l, t, r, b)
            val canScrollVertical = computeVerticalScrollRange() > height
            overScrollMode = if (canScrollVertical) OVER_SCROLL_ALWAYS else OVER_SCROLL_NEVER
        }
    }
    

提交回复
热议问题