方法一: 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. bzoj1293: [SCOI2009]生日礼物

    单调队列 用一个堆维护目前每个颜色在里面的点,每回取出队首点,并更新答案.一旦哪个颜色的点都被用完,跳出循环. #include<cstdio> #include<algorithm ...

  2. BZOJ_1021_[SHOI2008]_Debt循环的债务_(DP)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1021 三个人相互欠钱,给出他们每个人各种面额的钞票各有多少张,求最少需要传递多少张钞票才能把账 ...

  3. NOI2003 逃学的小孩

    这一题不会做啊…… 我觉得真要比赛的话我可能会随机上几万次,然后再用LCA求距离,更新最优值,等到快超时的时候输出答案…… 题解请看2007年陈瑜希论文 代码: ; type node=record ...

  4. php简单实现MVC

    在PHP中使用MVC越来越流行了,特别是在一些开源的框架当中.MVC足以应对大多数的情况,但还有一些情况是其不太适合的,如比较简单的个人博客,对于只有几百篇文章量级的博客,使用MVC让人觉得有些太复杂 ...

  5. 阿里云至 Windows Azure 的 Linux 虚拟机迁移

    在Windows Azure中,用户可以对部署在Azure中的虚拟机的映像.磁盘以及快照进行生成和下载.用户可以方便地将Azure中的虚拟机实例迁移到本地.私有云甚至其他公有云平台进行测试.扩展或者再 ...

  6. Java中的volatile

    关于volatile 在JVM 1.2之前,Java的内存模型实现总是从主存读取变量,是不需要进行特别的注意的.而随着JVM的成熟和优化,现在在多线程环境下 volatile关键字的使用变得非常重要. ...

  7. (十一)学习CSS之float属性

    参考:http://www.w3school.com.cn/cssref/pr_class_float.asp 定义和用法 float 属性定义元素在哪个方向浮动.以往这个属性总应用于图像,使文本围绕 ...

  8. GDI+ 学习记录(26): 显示图像 - Image

    //显示图像 var   g: TGPGraphics;   img: TGPImage; begin   g := TGPGraphics.Create(Self.Canvas.Handle);   ...

  9. textarea使用注意事项

    问题现象: 意外的发现页面中 textarea 标签中的内容缩进了 猜测: CSS影响了? 过程:(辛酸得说说) 查了CSS,并没有发现,CSS是正常的 然后找了一个正常的,跟这个异常的进行了对比,代 ...

  10. 【Spark学习】Apache Spark调优

    Spark版本:1.1.0 本文系以开源中国社区的译文为基础,结合官方文档翻译修订而来,转载请注明以下链接: http://www.cnblogs.com/zhangningbo/p/4117981. ...