Android之自定義ListView滾動條樣式

一、標明:不是快速滾動條(通過xml實現)

XML佈局使用 android:scrollbarThumbVertical

二、通過代碼方式實現:

找了找源碼,
//修改快速滾動條圖片
        try {
                        Field f = AbsListView.class.getDeclaredField("mFastScroller");
                        f.setAccessible(true);
                        Object o = f.get(listView);
                        f = f.getType().getDeclaredField("mThumbDrawable");
                        f.setAccessible(true);
                        Drawable drawable = (Drawable) f.get(o);
                        drawable = getResources().getDrawable(R.drawable.icon);
                        f.set(o, drawable);
                } catch (Exception e) {
                        e.printStackTrace();
                }
//修改豎向滾動條圖片
                
                try {
                        Field f = View.class.getDeclaredField("mScrollCache");
                        f.setAccessible(true);
                        Object scrollabilityCache  = f.get(listView);
                        f = f.getType().getDeclaredField("scrollBar");
                        f.setAccessible(true); 
                        Object scrollBarDrawable = f.get(scrollabilityCache);
                        f = f.getType().getDeclaredField("mVerticalThumb");
                        f.setAccessible(true); 
                        Drawable drawable = (Drawable) f.get(scrollBarDrawable); 
                        drawable = getResources().getDrawable(R.drawable.rating_progress);
                        f.set(scrollBarDrawable, drawable);
                } catch (Exception e) {
                        e.printStackTrace();
                }


使用ListView FastScroller,默認滑塊和自定義滑塊圖片的樣子:

  

 

設置快速滾動屬性很容易,只需在佈局的xml文件裏設置屬性即可:

[html] view plaincopy
  1. <ListView  
  2.     android:id="@+id/listView"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:fastScrollEnabled="true"  
  6.     android:focusable="true" />  


 

但是有時候會發現設置屬性無效,滾動ListView並未出現滑塊。原因是該屬性生效有最小記錄限制。當ListView記錄能夠在4屏以內顯示(也就是說滾動4頁)就不會出現滑塊。可能是api設計者認爲這麼少的記錄不需要快速滾動。

我的依據是android源代碼,見FastScroller的常量聲明:

[java] view plaincopy
  1. // Minimum number of pages to justify showing a fast scroll thumb  
  2. private static int MIN_PAGES = 4;  

以及:

[java] view plaincopy
  1. // Are there enough pages to require fast scroll? Recompute only if total count changes  
  2. if (mItemCount != totalItemCount && visibleItemCount > 0) {  
  3. mItemCount = totalItemCount;  
  4. mLongList = mItemCount / visibleItemCount >= MIN_PAGES;  
  5. }  

 

通篇查看了ListView及其超累AbsListView,都未找到自定義圖片的設置接口。看來是沒打算讓開發者更改了。但是用戶要求我們自定義這個圖片。那隻能用非常手段了。

經過分析發現,該圖片是ListView超類AbsListView的一個成員mFastScroller對象的成員 mThumbDrawable。這裏mThumbDrawable是Drawable類型的。mFastScroller是FastScroller類 型,這個類型比較麻煩,類的聲明沒有modifier,也就是default(package),只能供包內的類調用。

因此反射代碼寫的稍微麻煩一些:

[java] view plaincopy
  1. try {  
  2.     Field f = AbsListView.class.getDeclaredField(“mFastScroller”);  
  3.     f.setAccessible(true);  
  4.     Object o=f.get(listView);  
  5.     f=f.getType().getDeclaredField(“mThumbDrawable”);  
  6.     f.setAccessible(true);  
  7.     Drawable drawable=(Drawable) f.get(o);  
  8.     drawable=getResources().getDrawable(R.drawable.icon);  
  9.     f.set(o,drawable);  
  10.     Toast.makeText(this, f.getType().getName(), 1000).show();  
  11. catch (Exception e) {  
  12.     throw new RuntimeException(e);  
  13. }  
發佈了110 篇原創文章 · 獲贊 7 · 訪問量 12萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章