22

Can't find a way to remove the ViewPager2 overscroll shadow animation. I know on ViewPager, you can directly just set the overscrollMode attribute to never, however, it does not work on ViewPager2

Already tried the following

<androidx.viewpager2.widget.ViewPager2
        android:id="@+id/viewPager"
        android:layout_width="match_parent"
    android:layout_height="match_parent"
        android:overScrollMode="never"/>
binding.viewPager.apply {
        adapter = adapter
        orientation = ViewPager2.ORIENTATION_VERTICAL
        overScrollMode = ViewPager2.OVER_SCROLL_NEVER
        offscreenPageLimit = if (containsVideo) 2 else 5
}
Asue
  • 1,501
  • 1
  • 13
  • 22
Daniel Kim
  • 899
  • 7
  • 11

7 Answers7

45

Solution

binding.viewPager2.apply {
    adapter = vpAdapter
    orientation = ViewPager2.ORIENTATION_VERTICAL
    registerOnPageChangeCallback(pageChangeCallback)
    (getChildAt(0) as RecyclerView).overScrollMode = RecyclerView.OVER_SCROLL_NEVER
}
Daniel Kim
  • 899
  • 7
  • 11
  • Please provide solution without data binding – galaxigirl Feb 09 '20 at 12:45
  • 3
    @galaxigirl `findViewById(R.id.viewPagerIdFromLayout).apply { (getChildAt(0) as? RecyclerView)?.overScrollMode = RecyclerView.OVER_SCROLL_NEVER }` – Phehaz Apr 02 '20 at 14:22
  • 3
    Not sure about when this answer was originally written, but currently `overScrollMode` and `OVER_SCROLL_NEVER` are functions/constants on `View`, not `RecyclerView`. So the answer reduces to `fun ViewPager2.disableOverscroll() {getChildAt(0).overScrollMode = View.OVER_SCROLL_NEVER}` – tir38 Sep 15 '21 at 17:42
15

In case anyone searching for a Java solution

View child = viewPager2.getChildAt(0);
if (child instanceof RecyclerView) {
    child.setOverScrollMode(View.OVER_SCROLL_NEVER);
}
TreyWurm
  • 495
  • 5
  • 16
4

As Kotlin extension:

fun ViewPager2.removeOverScroll() {
    (getChildAt(0) as? RecyclerView)?.overScrollMode = View.OVER_SCROLL_NEVER
}

and you are using it in your Fragment/Activity:

viewPager.removeOverScroll()
Boken
  • 4,825
  • 10
  • 32
  • 42
1

In Java you can simply do it like this

viewpager2.getChildAt(0).setOverScrollMode(RecyclerView.OVER_SCROLL_NEVER);

Vinay K
  • 23
  • 7
  • What if the child referenced using `.getChildAt(0)` is not present in the Recycle View? It will throw an exception. The current code doesn't handles that case. Please [edit] to handle such case. – Abhishek Dutt Oct 13 '22 at 16:33
0

This one worked for me:

val child = binding.<your viewPager camelCase id>.getChildAt(0)
    (child as? RecyclerView)?.overScrollMode = View.OVER_SCROLL_NEVER
Ozzini
  • 145
  • 1
  • 7
0

My kotlin code version that work in my project, without binding:

// over scroll animation
val child: View = pager.getChildAt(0)

if (child is RecyclerView) {
    child.overScrollMode = View.OVER_SCROLL_NEVER
}

Thanks.

Paulo Coutinho
  • 705
  • 1
  • 7
  • 15
-1

Use android:overScrollMode="never"

 <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/infoViewPager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:overScrollMode="never"
        app:layout_constraintBottom_toTopOf="@id/guideLine1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />
Mubarak Tahir
  • 207
  • 3
  • 4