Android ListView快速滚动自定义问题

6

这是我的列表视图。

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

但是列表视图的快速滚动条仍然没有得到定制。


不要对您的ListView进行任何更改。创建样式,将其应用于您在清单中的Application标记。 - adneal
<application android:icon="@drawable/logo" android:label="@string/app_name" android:debuggable="true" style="@drawable/listviewfastscrollstyle"但仍然无法正常工作。 - user1155857
2个回答

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>

在你的清单文件中设置样式,像这样:

<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" />

样式文件的文件名也应该是CustomTheme.xml吗? - user1155857
当然我会接受 :) 但是仍然不起作用...这里是样式代码,我直接放在values文件夹中 <?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>
- user1155857
你的清单文件有误。将“style=@style…”更改为android:theme="@style/listviewfastscrollstyle",这正是我在帖子中所写的。 - adneal
我还想知道在蜂巢版本之前会是怎样的情况? - Sandra
有没有办法在运行时更改这个?除了setFastScrollStyle,因为它需要API 21... - JanBo
显示剩余9条评论

8
为了更改fastScrollThumbDrawablefastScrollTrackDrawable或快速滚动SectionIndexer的文本颜色,您需要使用上下文主题。其他答案建议通过AndroidManifest重写应用程序的主题来实现此目的。这确实有效,但是如果您希望每个ListView有不同的滚动条外观,则无法做到这一点。而且,您在应用程序主题中更改SectionIndexer的文本颜色的方式不应该这样做,因为它可能会产生其他不想要的影响。
最好的方法是创建一个使用ContextThemeWrapper的自定义ListView来为快速滚动设置样式。
下面是一个示例:
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>

据我所知,在HoneyComb之前(API 11),没有一种方法可以为FastScroll bar设置样式。

非常感谢您!您知道我是否可以更改使用此方法滚动时出现的标签文本大小吗? - MinaHany

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