1

In Android since API level 26 we can easily enable fast scrolling for RecyclerView. But all the examples found show only the use of 'fastScrollEnabled' property in the XML-layout.

How to change this property programmatically?

Gregory
  • 802
  • 10
  • 16
  • You can't. It can only be enabled from layout XML attributes. There's a request on the issue tracker to add some public method to do that, but it's been there for years, and doesn't seem a priority. – Mike M. Jul 03 '20 at 02:26
  • 1
    @MikeM. Ok, thanks! – Gregory Jul 03 '20 at 02:34
  • 1
    No problem. I should mention, though, that [the `FastScroller` class](https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/recyclerview/recyclerview/src/main/java/androidx/recyclerview/widget/FastScroller.java) is just an `ItemDecoration`/`OnItemTouchListener`, so there's no reason you can't copy that class into your project, and set it up yourself externally, as needed. I've done it myself once, and it's pretty simple, IIRC. – Mike M. Jul 03 '20 at 02:44
  • @MikeM. You did not override the RecyclerView class? – Gregory Jul 03 '20 at 04:52
  • Nope, no need to. All `RecyclerView` does internally is load the 4 drawables, and 3 other dimension resources, and then instantiates the `FastScroller` with those and itself. You can have a look [its `initFastScroller()` method](https://android.googlesource.com/platform/frameworks/support/+/androidx-master-dev/recyclerview/recyclerview/src/main/java/androidx/recyclerview/widget/RecyclerView.java#11786). That's the method `RecyclerView` calls from its constructor, and that's the only place it's ever done, which is why you're stuck with the XML attributes, for the built-in implementation, anyway – Mike M. Jul 03 '20 at 04:59
  • @MikeM. In this case, I will not be able to dynamically enable or disable 'fast scrolling mode' with my own FastScroller class for created RecyclerView object? – Gregory Jul 03 '20 at 06:04
  • 1
    @MikeM. Thanks! It works! Thanks, it works! I did as you recommended. I added a method to detach FastScroller from the RecyclerView and call it when it is required. – Gregory Jul 03 '20 at 07:52

0 Answers0