android listview快速滚动定制问题

时间:2012-02-08 05:40:32

标签: android listview fastscroll

这是我的listview

<ListView android:layout_width="match_parent"
            android:layout_height="match_parent" android:id="@+id/ListView"
            android:fastScrollEnabled="true" android:divider="@null"
            style="@drawable/listviewfastscrollstyle"
            ></ListView>

这是listviewfastscrollstyle样式文件

<style> 
<item name="android:fastScrollTrackDrawable">@drawable/listselector</item> 
<item name="android:fastScrollThumbDrawable">@drawable/listselector</item> 
</style>

这是listselector文件

<selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true" android:drawable="@drawable/channelarrows_down" /> 
 <item android:drawable="@drawable/minimize" /> 
 </selector>

但是列表视图快速滚动条仍未定制。

2 个答案:

答案 0 :(得分:9)

您创建的样式不正确。你需要给它一个名字。我不确定你上一篇文章发生了什么事。执行以下操作:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<style name="listviewfastscrollstyle" parent="android:Theme">
    <item name="android:fastScrollTrackDrawable">@drawable/listselector</item>
    <item name="android:fastScrollThumbDrawable">@drawable/listselector</item>
</style>

</resources>

在你的Manifest中设置这样的风格:

<application 
    android:icon="@drawable/icon" 
    android:label="@string/app_name" 
    android:theme="@style/CustomTheme">

根据要求,这是我正在测试的ListView:

<ExpandableListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="0dip"
    android:layout_weight="1"
    android:drawSelectorOnTop="false"
    android:fastScrollAlwaysVisible="true"
    android:fastScrollEnabled="true"
    android:indicatorLeft="8dip"
    android:indicatorRight="52dip"
    android:paddingRight="32dip" />

答案 1 :(得分:8)

要更改fastScrollThumbDrawablefastScrollTrackDrawable或快速滚动SectionIndexer的文本颜色,您必须使用上下文主题。其他答案建议通过AndroidManifest覆盖应用程序的主题来执行此操作。这确实有效但如果你想要每ListView个不同的滚动条外观,你就不能这样做。此外,更改SectionIndexer上的文字颜色的方式不应在您的应用主题中完成,因为它可能会产生其他不良影响。

为快速滚动设置ListView样式的最佳方法是创建使用ListView的自定义ContextThemeWrapper

以下是一个例子:

public class FastscrollThemedListView extends ListView {
    public FastscrollThemedListView(Context context, AttributeSet attrs) {
        super(new ContextThemeWrapper(context, R.style.FastScrollTheme), attrs);
    }
}

这就是你所需要的一切。你的风格如下:

<style name="FastScrollTheme">
    <item name="android:textColorPrimary">?android:textColorPrimaryInverse</item>
    <item name="android:fastScrollThumbDrawable">@drawable/fast_scrollbar_thumb</item>
    <item name="android:fastScrollTrackDrawable">@drawable/fast_scrollbar_track</item>
</style>

textColorPrimary如果您使用它,就会挂钩SectionIndexer字体颜色。

您的ListView将如下所示:

<com.yourapp.view.FastscrollThemedListView 
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:id="@+id/ListView"
    android:fastScrollEnabled="true" 
    android:divider="@null"/>

如果你需要它,这就是你的ThumbDrawable的样子:

<强> fast_scrollbar_thumb.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:top="10dp" android:bottom="10dp">
        <shape android:shape="rectangle">
            <size
                android:height="45dp"
                android:width="5dp" />
            <solid android:color="#DA414A" />
        </shape>
    </item>
</layer-list>

AFAIK没有为什么要在HoneyComb(API 11)之前设置FastScroll bar的样式