How to disable overscroll effect on RecyclerView ONLY when you can't scroll anyway ?
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"/>
Just add android:overScrollMode="never"
in XML
you could give OVER_SCROLL_IF_CONTENT_SCROLLS
a try. Accordingly to the documentation
Allow a user to over-scroll this view only if the content is large enough to meaningfully scroll, provided it is a view that can scroll.
Or you could check if you have enough items to trigger the scroll and enable/disable the over scroll mode, depending on it. Eg
boolean notAllVisible = layoutManager.findLastCompletelyVisibleItemPosition() < adapter.getItemCount() - 1;
if (notAllVisible) {
recyclerView.setOverScrollMode(allVisible ? View.OVER_SCROLL_NEVER);
}
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
}
}