Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 931
anujprashar Avatar asked Feb 05 '13 13:02

anujprashar


2 Answers

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.

like image 181
Michał Z. Avatar answered Nov 06 '22 22:11

Michał Z.


You can try setting the attribute

android:fastScrollAlwaysVisible="true"

in your listview xml

like image 43
sutoL Avatar answered Nov 06 '22 23:11

sutoL