12

我想自定义一个带有可触摸快速滚动条的列表视图,例如带有拇指图像的垂直线的谷歌播放音乐应用程序。它提供了一种简单快捷的方式来使用这个可触摸的快速滚动条进行滚动。我尝试搜索这样的自定义滚动条,但在列表视图中找不到任何具有快速滚动条的内容。我期待滚动条的输出如下图所示:

在此处输入图像描述

外面的快速滚动条用红线标记。我在 StackOverflow 上找到了这篇文章,但链接中的代码没有给我预期的输出。你能帮我做这件事吗?

4

2 回答 2

38

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

值/样式.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>

活动布局 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 文件:

可绘制/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>

最终输出如下图:

在此处输入图像描述

于 2013-06-27T11:14:14.377 回答
2

所以对于 android api level < 11 有特殊的 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();
            }
        }

此代码加上 android api 级别的解决方案 >= 11 = 所有 android api 级别的解决方案)

于 2014-03-18T17:01:39.723 回答