2012-02-19 219 views
2

我正在做一個應用程序,我希望我的listview上的fastscroll選項卡是一個自定義的拇指選項卡。閱讀文檔在線我認爲這將做到這一點:Android ListView fastScrollThumbDrawable不工作

<ListView 
android:id="@android:id/list" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:drawSelectorOnTop="false" 
android:cacheColorHint="@color/myWhite" 
android:scrollbars="vertical" 
android:scrollbarThumbVertical="@drawable/scrollthumb" 
android:scrollbarSize="12dip" 
android:fastScrollThumbDrawable="@drawable/scrollbar_thumb" 
android:fastScrollEnabled="true" 
/> 

列表視圖是罰款,自定義滾動條的顏色正在和fastscrollbar選項卡顯示,但它使用的是默認的拇指圖像,而不是PNG文件scrollbar_thumb

拇指圖像是否需要以某種格式或大小? 是否可以更改爲自定義圖形,如果不可以,至少可以更改拇指的顏色?

任何幫助將非常感激

回答

0

注意,屬性僅適用於Android API級別11及更高版本。

http://developer.android.com/reference/android/R.attr.html

+0

礦仍然無助於ICS 。我必須將主題應用到我的應用程序才能獲得自定義快速滾動條。 – sgarman 2012-05-15 22:48:22

+1

@sgarman,能否請你解釋一下(作爲答案吧?)你的意思是將你的應用程序的主題應用到自定義快速滾動大拇指上? – Rick 2012-09-12 15:19:35

2

在ListView XML定義,添加

android:fastScrollEnabled="true" 

或代碼

listView.setFastScrollEnabled(true); 

創建在res /可繪製文件夾文件fastscroll_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_pressed" /> 
    <item android:drawable="@drawable/fastscroll" /> 
</selector> 

在AndroidManifest.xml中,設置自定義主題,爲您的應用:

<application 
    android:theme="@style/ApplicationTheme" 
    ...> 

創建res文件夾一個文件夾值。創建RES /值的themes.xml文件內容如下:

<resources> 
    <style name="ApplicationTheme"> 
     <item name="android:fastScrollThumbDrawable">@drawable/fastscroll_thumb</item> 
    </style> 
</resources> 

最後確保fastscroll.png和fastscroll_pressed.png在繪製文件夾中存在

+0

我喜歡你的明確解釋。我不明白,爲什麼我需要在AndroidManifest中設置android:主題,而不是LinearLayout元素中的另一個佈局文件。 – 2015-02-19 23:27:12

+0

在編寫它的android版本中,更改快速滾動縮略圖的唯一方法是使用應用程序主題在整個應用程序中設置它。有可能在Android的更新版本中將其設置爲LinearLayout,但我將不得不檢查該內容並回復給您。 – JeffG 2015-05-07 19:43:51

0

我使用的是,但我不知道爲什麼它不工作,所以在網上搜索我發現here一個硬代碼解決方案,我不知道它是否適用於舊的API,但在我的情況是解決了這個問題。注意我正在使用API​​ 18,如目標和帶有API 17的設備來測試。

代碼:

try { 
    Field f = AbsListView.class.getDeclaredField("mFastScroller"); 
    f.setAccessible(true); 
    Object o = f.get(<<your listView here>>); 
    f = f.getType().getDeclaredField("mThumbDrawable"); 
    f.setAccessible(true); 
    Drawable drawable = (Drawable) f.get(o); 
    drawable = getResources().getDrawable(R.drawable.<<your thumb drawable here can be a selector>>); 
    f.set(o, drawable); 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
0

爲了改變fastScrollThumbDrawable,將fastScrollTrackDrawable,或fastscroll SectionIndexer你必須使用一個上下文主題的文本顏色。其他答案建議通過AndroidManifest覆蓋應用程序的主題來執行此操作。這確實有效,但如果你想要不同的滾動條外觀每ListView你不能這樣做。此外,您更改SectionIndexer上的文字顏色的方式不應在您的應用主題中完成,因爲它可能具有其他不良效果。

爲快速滾動設置ListView的最佳方式是創建一個使用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:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:drawSelectorOnTop="false" 
    android:cacheColorHint="@color/myWhite" 
    android:scrollbars="vertical" 
    android:scrollbarSize="12dip" 
    android:fastScrollEnabled="true"/> 
0

我測試了以前的答案,並與該簡化版瓦下爲快速滾動的自定義拇指圖標/最低要求上來:

Android清單: (應用標籤)

android:theme="@style/ApplicationTheme"

RES /值/的themes.xml:

<resources> 
    <style name="ApplicationTheme"> 
    <item name="android:fastScrollThumbDrawable">@drawable/scrollbar</item> 
    </style> 
</resources> 

佈局\ activity_main.xml中: (或任何你的觀點是,要將此應用到&實現快速滾動) 添加此attibute:

<ListView android:fastScrollEnabled="true" />