方法一: 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. poj2373

    其实这道题不是很难,不难想到f[i]表示覆盖到[0,i]的最少喷头数 很明显是一个dp+单调队列的问题 但是细节问题比较多,首先是不能覆盖到[0,l]外面,所以长度为奇数不能被完全覆盖 还有一些区间[ ...

  2. UVa 10213 (欧拉公式+Java大数) How Many Pieces of Land ?

    题意: 一块圆形土地,在圆周上选n个点,然后两两连线,问把这块土地分成多少块? 分析: 首先紫书上的公式是错的,不过根据书上提供的思路很容易稍加修改得到正确答案! 然后推公式吧,这里用到平面图的欧拉公 ...

  3. BZOJ2882: 工艺

    题解: 裸的字符串最小表示... 可以戳这里:http://www.cnblogs.com/ACAC/archive/2010/05/23/1742349.html 这里说一下为什么a[i+k]> ...

  4. 最受 Web 开发者欢迎的 NoSQL 和关系数据库

    Web应用离不开数据库,目前市场上有种类繁多数据库可供开发者选择,例如SQL.NoSQL.键值.图谱数据库等等.关于不同数据库在开发者中的受欢迎程度也是仁者见仁智者见智,但是通过统计亚马逊这样的公共云 ...

  5. DOM的定义及DOM相关

    DOM : Document Object Model 文档对象模型文档:html页面文档对象:页面中元素文档对象模型:定义 为了能够让程序(js)去操作页面中的元素 DOM会把文档看作是一棵树,同时 ...

  6. 建立自己的bin目录,在当前路径运行shell脚本

    Shell脚本nusers cat nusers #! /bin/sh - who | wc -l 如果你要编写自己的脚本,最好准备自己的bin目录来存放它们,并且让Shell能够自动找到它们.这不难 ...

  7. C#实现无物理边距真正可打印区域的绘图\打印程序开发

    经常在开发实际的应用程序中,需要用到图形绘制和打印程序.如何实现完整的精确打印和绘图是需要注意许多细节地方的.最近在遇到打印问题的时候,仔细研究一阵,总结这篇博文,写得有点杂乱,看文要还请费点神. 基 ...

  8. Dos操作

    \tree/f >c.txt \dir/s/b >c.txt

  9. uva 11020 Efficient Solutions

    题意:给你n个人,有两个属性x.y,如果不存在另外一个人x2,y2满足 x2<=x,y2<y 或者 x2<x,y2<=y,那么就称这个人是有优势的,每次给你一个人得信息,问你当 ...

  10. uva 11168

    题意:给出平面上的n个点,求一条直线,使得所有点在该直线的同一侧且所有点到该直线的距离和最小,输出该距离和. 思路:要使所有点在该直线的同一侧,明显是直接利用凸包的边更优.所以枚举凸包的没条边,然后求 ...