Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show custom thumb when using fastScrollEnabled

Tags:

android

I have a listview of countries in non-alphabetic order and started using fastscroll. I would like to display the country-flag when scrolling with fastscroll but it seems like the APIs has the FastScroll class as private so I cannot override it.

Have anyone else implemented a custom fastscroll view?

References: http://developer.android.com/reference/android/widget/AbsListView.html#attr_android:fastScrollEnabled

like image 885
Christer Nordvik Avatar asked Aug 14 '11 20:08

Christer Nordvik


1 Answers

In your ListView XML definition, add

android:fastScrollEnabled="true"

or in code

listView.setFastScrollEnabled(true);

Create file fastscroll_thumb.xml in the res/drawable folder as follows:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/fastscroll_pressed" />
    <item android:drawable="@drawable/fastscroll" />
</selector>

In AndroidManifest.xml, set a custom theme for your application:

<application
    android:theme="@style/ApplicationTheme"
    ...>

Create a values folder in the res folder. Create themes.xml files in res/values as follows:

<resources>
    <style name="ApplicationTheme">
        <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb</item>
    </style>
</resources>

Lastly make sure that fastscroll.png and fastscroll_pressed.png exist in your drawable folder

(optional) You can also set fast scroll always visible while you are debugging if you like

listView.setFastScrollAlwaysVisible(true);

or in XML

android:fastScrollAlwaysVisible="true"
like image 125
JeffG Avatar answered Oct 19 '22 13:10

JeffG