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

前端 未结 5 1042
囚心锁ツ
囚心锁ツ 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:08

    Longer workaround, based on here (to solve this issue), handles more cases, but still a workaround:

    /**a temporary workaround to make RecyclerView handle android:overScrollMode="ifContentScrolls"  */
    class NoOverScrollWhenNotNeededRecyclerView : RecyclerView {
        private var enableOverflowModeOverriding: Boolean? = null
        private var isOverFlowModeChangingAccordingToSize = false
    
        constructor(context: Context) : super(context)
        constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)
        constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
    
        override fun setOverScrollMode(overScrollMode: Int) {
            if (!isOverFlowModeChangingAccordingToSize)
                enableOverflowModeOverriding = overScrollMode == View.OVER_SCROLL_IF_CONTENT_SCROLLS
            else isOverFlowModeChangingAccordingToSize = false
            super.setOverScrollMode(overScrollMode)
        }
    
        override fun onLayout(changed: Boolean, l: Int, t: Int, r: Int, b: Int) {
            super.onLayout(changed, l, t, r, b)
            if (enableOverflowModeOverriding == null)
                enableOverflowModeOverriding = overScrollMode == View.OVER_SCROLL_IF_CONTENT_SCROLLS
            if (enableOverflowModeOverriding == true) {
                val canScrollVertical = computeVerticalScrollRange() > height
                val canScrollHorizontally = computeHorizontalScrollRange() > width
                isOverFlowModeChangingAccordingToSize = true
                overScrollMode = if (canScrollVertical || canScrollHorizontally) OVER_SCROLL_ALWAYS else OVER_SCROLL_NEVER
            }
        }
    }
    

提交回复
热议问题