方法一: SectionIndexer接口 + 索引列表

参考:http://www.apkbus.com/android-69999-1-1.html
所谓section 就是一组有共性的item, 比如由相同的字母开头

SectionIndexer接口主要的方法有:

实现步骤:

1.给listview添加section

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
public class MyAdapter extends BaseAdapter implements SectionIndexer , Filterable{

                private List<String> sections = new ArrayList<String>(); //保存section

                @Override

        public View getView(int position, View convertView, ViewGroup parent) {

                        //获取item控件

            。。。

            //添加section, 这里是将首字母相同的分为一个section
//section用一个TextView实现,需要分组的时候显示,不需要的时候隐藏 String label = filteredItems.get(position).get("cid");
char firstChar = label.toUpperCase().charAt(0);
if (position == 0) { viewholder.section.setVisibility(View.VISIBLE);
viewholder.section.setText(label.substring(0, 1).toUpperCase());
sections.add(label.substring(0, 1).toUpperCase()); } else { String preLabel = filteredItems.get(position - 1).get("cid");
char preFirstChar = preLabel.toUpperCase().charAt(0);
if (firstChar != preFirstChar) { viewholder.section.setVisibility(View.VISIBLE);
viewholder.section.setText(label.substring(0, 1).toUpperCase());
sections.add(label.substring(0, 1).toUpperCase()); } else { viewholder.section.setVisibility(View.GONE); } }
return convertView; } @Override public Object[] getSections() { return sections.toArray(); } @Override
public int getPositionForSection(int section) { if (section == '!') { //这里第一行是个搜索框,用个"!"做个标记 return 0; } else { //其余的都按首字母查找,查找时都换成大写,用于忽略大小写匹配 for (int i = 0; i < filteredItems.size(); i++) { String l = filteredItems.get(i).get("cid");
char firstChar = l.toUpperCase().charAt(0);
if (firstChar == section) { return i+1; } } }
return -1; } @Override
public int getSectionForPosition(int position) { return 0; } }

2.创建索引列表
这个因人而异,看需求了。上面我的section是用首字母区分的,那么索引就用字母来做。
可以直接定死了26个字母。也可以调用adapter. getSections()获取到所有的section,生成索引。
关于生成索引,参考的例子中是做成了一个SideBar的控件,这个还有待研究,我只能看得懂,自己写不出来。

参考的例子中是:
```Java
@Override

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
protected void onDraw(Canvas canvas) {

    Paint paint = new Paint();
paint.setColor(color);
paint.setTextSize(12);
paint.setStyle(Style.FILL);
paint.setTextAlign(Paint.Align.CENTER);
float widthCenter = getMeasuredWidth() / 2;
if (l.length > 0) { float height = getMeasuredHeight() / l.length;
for (int i = 0; i < l.length; i++) { if (i == 0 && type != 2) { canvas.drawBitmap(mbitmap, widthCenter - 7, (i + 1)* height - height / 2, paint); } else { canvas.drawText(String.valueOf(l[i]), widthCenter, (i + 1) * height, paint); } } }
this.invalidate();
super.onDraw(canvas); }
1
2
3
3.索引关联listview的section
点击某个索引值的时候,调用getPositionForSection()获得改索引对应的第一个position,然后设置```Java
listview.setSelection(position);

该参考例子中的实现是:
OnTouch事件中获取SectionIndexer当前的position, 并将当前选中的section显示出来

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  @Override
public boolean onTouchEvent(MotionEvent event) { super.onTouchEvent(event);
int i = (int) event.getY(); int idx = i / (getMeasuredHeight() / l.length);
if (idx >= l.length) { idx = l.length - 1; } else if (idx < 0) { idx = 0; }
if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) { setBackgroundResource(R.drawable.scrollbar_bg); //显示当前选中的section
mDialogText.setVisibility(View.VISIBLE); if (idx == 0) { mDialogText.setText("Search");
mDialogText.setTextSize(16); } else { mDialogText.setText(String.valueOf(l[idx]));
mDialogText.setTextSize(34); } //获取sectionIndexer对象
if (sectionIndexter == null) { sectionIndexter = (SectionIndexer) list.getAdapter(); } //获取当前section对应的position
int position = sectionIndexter.getPositionForSection(l[idx]);
if (position == -1) { return true; } //设置listview当前选中该position list.setSelection(position); } else { mDialogText.setVisibility(View.INVISIBLE); } if (event.getAction() == MotionEvent.ACTION_UP) { setBackgroundDrawable(new ColorDrawable(0x00000000)); }
return true; }

listview和显示当前section的textview都是外面activity传过来的

 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
  public void setListView(ListView _list) {

        list = _list;
HeaderViewListAdapter ha = (HeaderViewListAdapter) _list.getAdapter();
MyAdapter ad = (MyAdapter)ha.getWrappedAdapter();
sectionIndexter = (SectionIndexer)ad; } public void setTextView(TextView mDialogText) { this.mDialogText = mDialogText; }

效果图:

Android ListView快速定位(一)的更多相关文章

  1. Android ListView快速定位(三)

    方法三: android:fastScrollEnabled="true" 这个很简单,只要把属性设置了,就可以起作用了 不过这个滑块比较丑,当然网上也有自定义图片的例子. 参考 ...

  2. Android ListView快速定位(二)

    方法二:android:textFilterEnabled="true" + Filter 这个属性在android.widget.AbsListView下,要求adapter必须 ...

  3. Android ListView快速定位(四)

    方法四: 添加一个EditText,作为搜索框 + Filter 其实这个不算第四个方法,因为与第二个一样,主要是实现Filter. 但是对于EditText的监听,我以前也没有写过,所以也记录一下. ...

  4. Android apk快速定位、灰色按钮克星--DroidSword

    本文博客地址:https://blog.csdn.net/QQ1084283172/article/details/80994434 在进行Android应用程序的逆向分析时,经常需要对Android ...

  5. [Android Studio] Android Studio快速定位当前打开的文件在哪个目录(package)下

    转载自:http://blog.csdn.net/hyr83960944/article/details/38067499 在Eclipse中有一个很好的功能,就是比如我打开一个AActivity,左 ...

  6. [Android Studio] Android Studio快速定位当前打开的文件在哪个目录(package)下

    转载自:http://blog.csdn.net/hyr83960944/article/details/38067499 在Eclipse中有一个很好的功能,就是比如我打开一个AActivity,左 ...

  7. 快速定位 Android APP 当前页面的三种方法(Activity / Fragment)

    方法一.通过adb命令打印当前页面: Android 如何快速定位当前页面是哪个Activity or Fragment (1)查看当前Activity :adb shell "dumpsy ...

  8. Android ListView A~Z快速索引(改进版)

    上一篇文章虽然实现了ListView 快速索引的效果,但是有一个小小的Bug.这个Bug我在前面也说了,这篇文章就来解决这个Bug. 我研究的时候发现只要showBg值为true,中间的字母就显示,而 ...

  9. Android GIS开发系列-- 入门季(10) MapView快速定位到Geometry

    我们知道某个Geometry的坐标,但不知道具体的位置,该如何使地图快速定位呢?这时需要用到MapView.setExtent方法,来看下这个方法的介绍:Zooms the map to the gi ...

随机推荐

  1. Web打印控件smsx.cab使用说明

    在项目开发中,经常会用到页面打印的功能,在ASP.NET环境下推荐一款web打印控件smsx.cab.    使用方法:一般会先定义一个用于打印的母版页(Print.Master),在母版页上做好布局 ...

  2. POJ 2398 Toy Storage

    这道题和POJ 2318几乎是一样的. 区别就是输入中坐标不给排序了,=_=|| 输出变成了,有多少个区域中有t个点. #include <cstdio> #include <cma ...

  3. hdu 2844 Coins

    Coins Time Limit: / MS (Java/Others) Memory Limit: / K (Java/Others) Total Submission(s): Accepted S ...

  4. Spring入门之HelloSpring

    Spring描述: -轻量级:Spring是非侵入式的-基于Spring开发的应用中的对象可以不依赖于Spring的API -依赖注入(DI---dependency injection,IOC) - ...

  5. HDU 5874 Friends and Enemies 【构造】 (2016 ACM/ICPC Asia Regional Dalian Online)

    Friends and Enemies Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Othe ...

  6. CentOS7 安装98五笔输入法

    86版的安装方式,网上找到一堆,折腾了很久才把98版的安装上,记录一下. 从这里下了这个 http://bbs.chinaunix.net/forum.php?mod=viewthread&t ...

  7. Python【基础第一篇】

    一.Python3新特性 编码统一为unicode Python3不支持Twisted,暂时只支持73% 1/2=0.5 print "hello World" 变成 print ...

  8. SQL Server优化相关的工具脚本

    SQL Server性能优化的一些常用脚本,适用于SQL Server 2008,更高的版本某些系统表的字段有所不同,建议参考MSDN. 死锁相关 /************************* ...

  9. HDU-4716 A Computer Graphics Problem 水题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4716 直接搞.. //STATUS:C++_AC_0MS_288KB #include <fun ...

  10. HDU 3436--Queue-jumpers (树状数组 or Splay Tree)

    树状数组这个真心想了好久,还是没想出来 %%% www.cppblog.com/Yuan/archive/2010/08/18/123871.html 树状数组求前缀和大于等于k的最大值,第一次看到这 ...