Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Listview Fast Scrollbar in android

Tags:

android

I want to custom a listview with a touchable fast scrollbar like Google play music app with vertical line with thumb image. It provides an easy and fast way to scroll with this touchable fast scrollbar. I tried searching for custom scrollbar like this, but I couldn't find any with fast scrollbar in listview. I am expecting the output of the scrollbar like the below figure:

enter image description here

The fast scrollbar outside is marked in red line. I have found this post on StackOverflow, but the code in the link doesn't give me the expected output. Can you help me do this?

like image 292
Stack Overflow User Avatar asked Jun 27 '13 07:06

Stack Overflow User


2 Answers

Finally, I came up with a solution. It only works with API level 11 or higher

values/style.xml

<style name="AppTheme" parent="@style/Theme.Sherlock.Light">
        <!-- All customizations that are NOT specific to a particular API-level can go here. -->
        <item name="android:fastScrollThumbDrawable">@drawable/fast_thumb</item>
        <item name="android:fastScrollTrackDrawable">@drawable/fastscroll_track_default_holo_dark</item>

</style>

Apply activity for this theme like below code:

         <activity
            android:name="com.example.viewpager.FirstActivity"
            android:theme="@style/AppTheme"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

Activity layout XML like below code:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/rlScrollingPlayList"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:orientation="vertical" >

        <ListView
            android:id="@+id/listView1"
            android:layout_width="match_parent"
            android:fastScrollEnabled="true"
            android:fastScrollAlwaysVisible="true"
            android:scrollbarStyle="outsideInset"
            android:layout_height="match_parent" >
        </ListView>

</RelativeLayout>

Resource image files are

enter image description hereenter image description hereenter here

Fast scroll thumb selector XML file:

drawable/fast_thumb.xml

<?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_thumb_pressed_holo"/>
    <item android:drawable="@drawable/fastscroll_thumb_default_holo"></item>

</selector>

Final output like below figure:

enter image description here

like image 59
Stack Overflow User Avatar answered Oct 03 '22 01:10

Stack Overflow User


so for android api level < 11 there is special hack

// special hack for violet fast scroll
        if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
            try {
                java.lang.reflect.Field f = AbsListView.class.getDeclaredField("mFastScroller");
                f.setAccessible(true);
                Object o = f.get(root.findViewById(R.id.beam_contact_listview));
                f = f.getType().getDeclaredField("mThumbDrawable");
                f.setAccessible(true);
                Drawable drawable = (Drawable) f.get(o);
                drawable = getResources().getDrawable(R.drawable.sv_fastscroll);
                f.set(o, drawable);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

this code plus solution for android api level >= 11 = solution for all android api levels )

like image 20
xoxol_89 Avatar answered Oct 03 '22 00:10

xoxol_89