anujprashar
anujprashar

Reputation: 6335

Android Listview code for enabling Fast Scroll in android framework source code

In android setFastScrollEnabled(true); is used for making ListView fast scroll.

This fast scroll does not work when there are less items in ListView. I read it somewhere that fast scroll in android works only when listview total height is 4 times or more than listview visible height. I have spent hours trying to find it in framework source code, but I am not able to find it.

Can someone point me to place in android framework source code where this condition to disable fast scroll when there are less items in ListView.

Upvotes: 4

Views: 4989

Answers (3)

DJ256
DJ256

Reputation: 101

Trying to adapt the answer from Michał Z., I finally ended up doing this :

listView.addOnLayoutChangeListener { _, _, _, _, _, _, _, _, _ ->
    val numberOfPages: Int = listView.count / listView.visibleItemsCount
    listView.isFastScrollAlwaysVisible = numberOfPages > THRESHOLD
}

It works just fine for me

Upvotes: 0

sutoL
sutoL

Reputation: 1767

You can try setting the attribute

android:fastScrollAlwaysVisible="true"

in your listview xml

Upvotes: 1

Michał Z.
Michał Z.

Reputation: 4119

Yes ofcourse, here is the link:

http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/1.5_r4/android/widget/FastScroller.java

This is a condidion between lines 224-227. And for setting how many pages it will be needed to show fast scroll, there is a constant:

private static int MIN_PAGES = 4;

And about disabling it... It's a private field so there is no simply way to do it. You can try use reflections or create custom FastScroller based on original. But i think the simpliest way is to check like in this condidion in Android code:

//pseudocode
int numberOfPages = listView.itemsCount / listView.visibleItemsCount;
if(numberOfPages > yourValue)
    listView.setFastScrollEnabled(true);
else
    listView.setFastScrollEnabled(false);

But it may only work if yourValue will be greater than 4. If you want to do it for less values then you need to use reflection or create custom class.

EDIT:

For newest version there is the link: http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/widget/FastScroller.java/

And lines are 444-447 :)

And for reflections I would try something like this:

try {
Field scrollerField = AbsListView.class.getDeclaredField("mFastScroller");    //java.lang.reflect.Field
scrollerField.setAccessible(true);
FastScroller instance = scrollerField.get(listViewInstance);

Field minPagesField = instance.getClass().getDeclaredField("MIN_PAGES");
minPagesField.setAccessible(true);
minPagesField.set(instance, yourValue);
} catch (Exception e) {
Log.d("Error", "Could not get fast scroller");
}

It's not tested so i don't know if it really works.

Upvotes: 8

Related Questions