使用Style自定义ListView快速滑动图标

蔚落 2022-09-19 14:29 206阅读 0赞

一、显示ListView快速滑动块图标

  1. 设想这样一个场景,当ListView的内容有大于100页的情况下,如果想滑动到第80页,用手指滑动到指定位置,无疑是一件很费时的事情,如果想快速滑动到指定的位置,只需加上ListViewfastScrollEnabled属性等于true,启用快速滑动功能即可。
  2. <ListView
  3. android:id="@android:id/list"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:fastScrollEnabled="true" />

注意:**默认只有当ListView的内容大于4页时,才会显示快速滑动块。**

读者可能会问,我是怎么知道要大于4页时,ListView才会显示快速滑动块图标。想知道原因,查一下ListView源码中fastScrollEnabled这个属性是怎么被初始化的吧。咱们到Eclipse中按ctrl+shift+t打开ListView的源码(前提是要和源码关联才能看到),你发现在ListView中并没有fastScrollEnabled这个属性,那就去父类AbsListView中找找吧,发现在AbsListView的构造方法中发现了初始化快速滑动块的代码(4.1.2源码在788-789行):

  1. boolean enableFastScroll = a.getBoolean(R.styleable.AbsListView_fastScrollEnabled, false);
  2. setFastScrollEnabled(enableFastScroll);

下面定位到setFastScrollEnabled方法:

  1. /**
  2. * Enables fast scrolling by letting the user quickly scroll through lists by
  3. * dragging the fast scroll thumb. The adapter attached to the list may want
  4. * to implement {@link SectionIndexer} if it wishes to display alphabet preview and
  5. * jump between sections of the list.
  6. * @see SectionIndexer
  7. * @see #isFastScrollEnabled()
  8. * @param enabled whether or not to enable fast scrolling
  9. */
  10. public void setFastScrollEnabled(boolean enabled) {
  11. mFastScrollEnabled = enabled;
  12. if (enabled) {
  13. if (mFastScroller == null) {
  14. mFastScroller = new FastScroller(getContext(), this);
  15. }
  16. } else {
  17. if (mFastScroller != null) {
  18. mFastScroller.stop();
  19. mFastScroller = null;
  20. }
  21. }
  22. }

从setFastScrollEnabled方法得知,ListView的快速滑动块是通过FastScroller这个类来创建的,接下来打开FastScroller的构造方法,来看下它创建滑动块的流程。(FastScroller这个类在Eclipse关联的源码中看不到,要到Android Framework源码中才能找到,Demo下载地址中包含了这个文件),下面我们来看看FastScroller这个类的关键代码:

  1. package android.widget;
  2. class FastScroller {
  3. //....省略部分源码
  4. // Minimum number of pages to justify showing a fast scroll thumb
  5. private static int MIN_PAGES = 4;
  6. private static final int THUMB_DRAWABLE = 1;
  7. private static final int[] ATTRS = new int[] {
  8. android.R.attr.fastScrollTextColor,
  9. android.R.attr.fastScrollThumbDrawable, // 定义快速滑动块图标的属性
  10. android.R.attr.fastScrollTrackDrawable,
  11. android.R.attr.fastScrollPreviewBackgroundLeft,
  12. android.R.attr.fastScrollPreviewBackgroundRight,
  13. android.R.attr.fastScrollOverlayPosition
  14. };
  15. //....省略部分源码
  16. private Drawable mThumbDrawable;// 快速滑动块图标
  17. public FastScroller(Context context, AbsListView listView) {
  18. mList = listView;
  19. init(context);
  20. }
  21. private void init(Context context) {
  22. // Get both the scrollbar states drawables
  23. TypedArray ta = context.getTheme().obtainStyledAttributes(ATTRS);
  24. useThumbDrawable(context, ta.getDrawable(THUMB_DRAWABLE)); // 获取当前主题ListView的快速滑动块图标
  25. //....省略部分源码
  26. }
  27. private void useThumbDrawable(Context context, Drawable drawable) {
  28. mThumbDrawable = drawable;
  29. if (drawable instanceof NinePatchDrawable) {
  30. mThumbW = context.getResources().getDimensionPixelSize(
  31. com.android.internal.R.dimen.fastscroll_thumb_width);
  32. mThumbH = context.getResources().getDimensionPixelSize(
  33. com.android.internal.R.dimen.fastscroll_thumb_height);
  34. } else {
  35. mThumbW = drawable.getIntrinsicWidth();
  36. mThumbH = drawable.getIntrinsicHeight();
  37. }
  38. mChangedBounds = true;
  39. }
  40. void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount,
  41. int totalItemCount) {
  42. // Are there enough pages to require fast scroll? Recompute only if total count changes
  43. if (mItemCount != totalItemCount && visibleItemCount > 0) {
  44. mItemCount = totalItemCount;
  45. mLongList = mItemCount / visibleItemCount >= MIN_PAGES; // 至少4页才显示滑动块
  46. }
  47. if (mAlwaysShow) { // android:fastScrollAlwaysVisible="true"
  48. mLongList = true;
  49. }
  50. //....省略部分源码
  51. }
  52. //....省略部分源码
  53. }

由上述代码得知,快速滑动块图标是由fastScrollThumbDrawable定义的,第8行定义了显示快速滑动块的最少页数,第51行onScroll方法负责处理显示快速滑动块的逻辑。

二、自定义快速滑动块图标

  1. eoe上看到有个[贴子][Link 1]通过反射,动态修改FastScroller对象的mThumbDrawable属性来改变快速滑动块的图标,这也不为于一种实现方式,但反射的效率较低。下面将介绍使用Style的方式来自定义图标。
  2. FastScroller类的init方法中可以得知,mThumbDrawable是通过获取当前Activity主题的**android.R.attr.fastScrollThumbDrawable**属性赋值,既然是这样的话,我们完全可以自定义一个主题,覆盖android.R.attr.fastScrollThumbDrawable属性对应的Drawable不就搞定了!

1、定义一个主题

  1. <style name="ListViewFastScrollThumb" parent="@android:style/Theme.Light.NoTitleBar.Fullscreen">
  2. <item name="android:fastScrollThumbDrawable">@drawable/ic_launcher</item>
  3. </style>

2、当前ListView所在Activity应用自定义的主题

  1. <activity
  2. android:name="com.example.actionbardemo.MainActivity"
  3. android:label="@string/app_name"
  4. android:theme="@style/ListViewFastScrollThumb" >
  5. <intent-filter>
  6. <action android:name="android.intent.action.MAIN" />
  7. <category android:name="android.intent.category.LAUNCHER" />
  8. </intent-filter>
  9. </activity>

3、验证

  1. public class MainActivity extends ListActivity {
  2. private static final int[] ATTRS = new int[] {
  3. android.R.attr.fastScrollThumbDrawable,
  4. };
  5. @Override
  6. protected void onCreate(Bundle savedInstanceState) {
  7. super.onCreate(savedInstanceState);
  8. setContentView(R.layout.activity_main);
  9. setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, Cheeses.sCheeseStrings));
  10. ImageView imageView = (ImageView) findViewById(R.id.fastScrollDrawable);
  11. Theme theme = getTheme();
  12. TypedArray a = theme.obtainStyledAttributes(ATTRS);
  13. Drawable drawable = a.getDrawable(0);
  14. imageView.setBackgroundDrawable(drawable);
  15. }
  16. }

布局:

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:id="@+id/container"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. android:orientation="vertical" >
  7. <ImageView
  8. android:id="@+id/fastScrollDrawable"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content" />
  11. <ListView
  12. android:id="@android:id/list"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent"
  15. android:fastScrollEnabled="true"
  16. />
  17. </LinearLayout>

4、 原生的和自定义的快速滑动块效果图对比:

SouthEast SouthEast 1

Demo下载地址:http://download.csdn.net/detail/xyang81/6788411

发表评论

表情:
评论列表 (有 0 条评论,206人围观)

还没有评论,来说两句吧...

相关阅读

    相关 mui定义图标

    研究了一下自定义图标,不仅仅可以在h5里面用,还可以在小程序或者别的开发环境里面用。 首先找到图标源,我自己找的是阿里巴巴矢量图库。 ![watermark_type_Zm