2013-06-27 31 views
11

Tôi muốn tùy chỉnh chế độ xem danh sách bằng thanh cuộn nhanh có thể chạm được như ứng dụng Google play music với đường thẳng đứng có hình ngón tay cái. Nó cung cấp một cách dễ dàng và nhanh chóng để cuộn với thanh cuộn nhanh có thể chạm này. Tôi đã cố gắng tìm kiếm cho scrollbar tùy chỉnh như thế này, nhưng tôi không thể tìm thấy bất kỳ với scrollbar nhanh trong listview. Tôi đang chờ đợi đầu ra của cuộn như hình bên dưới:Custom Listview Fast Scrollbar trong android

enter image description here

Các thanh cuộn nhanh bên ngoài được đánh dấu trong đường đỏ. Tôi đã tìm thấy this post trên StackOverflow, nhưng mã trong liên kết không cho tôi kết quả mong đợi. Bạn có thể giúp tôi làm điều này không?

+0

u có thể bài mã ur? – KOTIOS

Trả lời

36

Cuối cùng, tôi đã đưa ra một giải pháp. Nó chỉ làm việc với API mức 11 hoặc cao hơn

giá trị/style.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> 

Áp dụng hoạt động cho chủ đề này như dưới đây mã:

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

Hoạt động bố trí XML như dưới đây mã:

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

Resource tập tin hình ảnh

enter image description hereenter image description hereenter here

nhanh cuộn chọn tập tin thumb XML:

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

cuối cùng đầu ra như dưới đây con số:

enter image description here

+0

Trong trường hợp ai đó sẽ cần phải loại bỏ FastScrollTrackDrawable >> @ null Eftekhari

+0

câu trả lời hay, tuy nhiên bộ chọn không hoạt động trong trường hợp của tôi. –

2

để cho cấp api android < 11 có đặc biệt 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(); 
      } 
     } 

mã này cộng với giải pháp cho cấp api android> = 11 = giải pháp cho tất cả các cấp api android)