87

setOnScrollListener is deprecated. Reading the docs from here it tells to Use addOnScrollListener(OnScrollListener) and removeOnScrollListener(OnScrollListener).

What is the proper way to add and remove OnScrollListener?

Do i need to use removeOnScrollListner in onPause and set it up again using addOnScrollListener in onResume??

Martin Zeitler
  • 1
  • 19
  • 155
  • 216
Kushal Sharma
  • 5,978
  • 5
  • 25
  • 41

2 Answers2

135

addOnScrollListener(OnScrollListener) means you can add more than one listener to a RecyclerView.

removeOnScrollListener(OnScrollListener) means you can remove one of the listeners that attached to a specific RecyclerView. If the arg was set null, it equals to clearOnScrollListeners() method.

And, clearOnScrollListeners() let you remove all the listener from a RecyclerView.

MewX
  • 4,232
  • 1
  • 29
  • 38
  • 1
    Thank you, but i already understand the concept.. I want to know a proper use case of `add and remove OnScrollListener` – Kushal Sharma May 13 '15 at 06:36
  • 1
    @rogerthatcode Look at this: [add listener vs set listener](http://stackoverflow.com/a/7181144/4206925) and [Widgets should support more than one listener for each event type.](http://code.google.com/p/android/issues/detail?id=5711). I think one callback can handle everything, but more callbacks just let the listeners reusable, just like interface in java. One class can implement more than one interfaces, while each interface is available to any class. Hmm, just what I understand. – MewX May 13 '15 at 16:48
1

Replace setOnScrollListener with addOnScrollListener

and it will work fine