安卓中的自定义列表视图快速滚动条

12

我希望自定义一个可触摸的快速滚动条的列表视图,类似于Google Play音乐应用程序中带有垂直线和拇指图像的滚动条。使用这个可触摸的快速滚动条可以提供一种简单快捷的滚动方式。我尝试搜索类似的自定义滚动条,但是在列表视图中找不到任何带有快速滚动条的内容。我期望滚动条的输出如下图所示:

enter image description here

快速滚动条位于外部,用红线标记。我在StackOverflow上找到了这篇帖子,但链接中的代码没有给我期望的输出。你能帮我做这个吗?
2个回答

38

最终,我想出了一个解决方案。它只适用于API级别11或更高

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>

使用以下代码为此主题应用活动:

         <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布局XML:

<?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>

资源图像文件是:

输入图片描述 输入图片描述 输入图片描述

快速滚动拇指选择器 XML 文件:

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>

最终输出结果如下图所示:

enter image description here


如果有人需要删除FastScrollTrackDrawable,则可以使用以下代码:<item name="android:fastScrollTrackDrawable">@null</item> - Eftekhari
很好的回答,但在我的情况下选择器不起作用。 - user6649667

2

因此,对于Android API级别 < 11,存在特殊的技巧。

// 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();
            }
        }

以下代码是针对 Android API Level >= 11 的解决方案,也适用于所有 Android API 级别的解决方案:


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接