方法一: 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. Ruby类,模块1

    类的扩展和继承 class Fixnum def dosome(str) puts str end def abs puts "覆盖了原有的方法" end end puts 1.c ...

  2. 如何使用spring中的Log4jConfigListener--删除

    使用spring中的Log4jConfigListener有如如下好处:    1. 动态的改变记录级别和策略,不需要重启Web应用,如<Effective Enterprise Java> ...

  3. gem install走代理,速度刚刚的

    有个树莓pi,安装了shadowsocks 和 cow ,做代理,走ipv6,学校不收ipv6流量钱.速度也不错,快的下载可达10M/s. gem install xx遇到墙了. nano ~/.ge ...

  4. 魅族MX2代理设置

    魅族MX2买了快2年了,今天才知道有这个功能,唉 连接一个无线网络,比如我的centos 长按网络名字 选代理设置,设置自己的代理,再也不用SS 或 VPN 的android端了,老是提示ROOT权限 ...

  5. Convert to Objective-C ARC

    今天在进行代码走查时,竟然发现了下面这段代码: Bad Code 顿时感觉吐槽无力,虽然我反复强调内存管理问题,无非就是谁申请谁释放,利用强弱引用避免 retain-cycles,但是还是会有这样那样 ...

  6. 多线程与网络之cookies

    1. 网络请求中的cookie 1.1 删除cooki NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCook ...

  7. 自己手动写http服务器(2)

    tringBuilder response =new StringBuilder(); //1) HTTP协议版本.状态代码.描述 response.append("HTTP/1.1&quo ...

  8. bzoj 2244 [SDOI2011]拦截导弹(DP+CDQ分治+BIT)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=2244 [题意] 给定n个二元组,求出最长不上升子序列和各颗导弹被拦截的概率. [思路] ...

  9. ZOJ-3380 Patchouli’s Spell Cards DP, 组合计数

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3380 题意:有m种不同的元素,每种元素都有n种不同的相位,现在假 ...

  10. [学姿势]实验室搬砖+node学习

    这周开始进行收尾工作,我当然没有进行核心技术的开发,主要负责的是对web端进行展示上的修修补补,主要包括添加VLC播放器.rtsp视频流以及一些js细节. 1.VLC 全称为Video Lan Cli ...