最近需要实现一个城市列表的快速索引功能。类似于联系人应用,根据姓名首字母快速索引功能。

要实现这个功能只需要解决两个问题:
1、对列表进行分组(具有同一特征),并且能够快速定位到该组的第一项 2、右侧分组‘特征’快速索引栏的实现
第一个问题比较好解决,列表项可以根据相同的‘特征’来分组,比如说城市列表可以根据相同首字母的城市名来进行分组。 如何来定位到分组的第一项,只需要把分组的‘特征’和分组第一项下标关联起来,快速索引栏就能快速定位分组第一项
第二个问题可以通过自定义控件来实现,实现的形式有很多,可以通过绘制一整张分组‘特征’的图片(难以扩展),也可以动态来绘制每一个分组‘特征’(可扩展性强)
下面是一些实现的关键代码,基本上就是上面思想的体现,如果自己有特殊的需求话,稍做改动就能行

列表‘特征’和分组首项进行关联

1
2
3
4
5
6
7
8
9
for (int i = 0; i < mCitys.size(); i++) {
    City city = mCitys.get(i);
    String cityId = city.getId();
    if(cityId == null || .equals(cityId)) continue;
    String section = cityId.toUpperCase().substring(0, 1);
    if(!indexMap.containsKey(section)){
        indexMap.put(section, i);
    }
}

快速索引的绘制

1
2
3
4
5
6
7
8
9
@Override
protected void onDraw(Canvas canvas) {
    heightCenter = getMeasuredHeight()/2 - preHeight*WORDS.length/2;
    for (int i = 0; i < WORDS.length; i++) {
        canvas.drawText(String.valueOf(WORDS[i]), getMeasuredWidth()/2, preHeight
                + (i * preHeight) + heightCenter, mPaint);
    }
    super.onDraw(canvas);
}

用户交互,快速定位到索引项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public boolean onTouchEvent(MotionEvent event) {
    int startY = (int) event.getY() - heightCenter;
    int index = startY / preHeight;
    if (index >= WORDS.length) {
        index = WORDS.length - 1;
    } else if (index < 0) {
        index = 0;
    }
    if (event.getAction() == MotionEvent.ACTION_DOWN || event.getAction() == MotionEvent.ACTION_MOVE) {
        int position = mSectionIndexter.getStartPositionOfSection(String.valueOf(WORDS[index]));
        if (position == -1) {
            return true;
        }
        mListView.setSelection(position);
    }
    return true;
}

效果图如下:

结伴旅游,一个免费的交友网站:www.jieberu.com

推推族,免费得门票,游景区:www.tuituizu.com

Android 快速索引(城市列表和联系人)的更多相关文章

  1. Android-自定义联系人快速索引

    效果图: 布局去指定 view.custom.shangguigucustomview.MyCustomIndexView 自定义View对象 <!-- 自定义联系人快速索引 --> &l ...

  2. Android例子源码非第三方实现根据字母排序的城市列表

    values 下dimens.xml <resources> <!-- Default screen margins, per the Android Design guidelin ...

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

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

  4. Android 国际区号注册手机号编码 以及常用城市列表

    附上 国际区号编码:我是定义到arrays.xml里面了 <?xml version="1.0" encoding="utf-8"?> <re ...

  5. 实现ListView A~Z快速索引

    ListView A~Z快速索引这种效果在通信录和城市列表中经常看到,方便用户查找,是一种增加用户体验的好方法. 实现步骤: 1.自定义一个名叫SlideBar 的View. 2.在布局文件中加入这个 ...

  6. 8.快速索引、listview

    实现这样的效果 布局: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" ...

  7. 快速索引 (对View的自定义)

    快速索引 (对View的自定义) 快速索引应用场景: 微信好友列表, 联系人通讯录, 应用管理, 文件管理等. 快速索引7步曲: *1. A-Z索引的绘制. * 2. 处理Touch事件. * 3. ...

  8. [译]:Xamarin.Android开发入门——Hello,Android快速上手

    返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...

  9. 获取中央气象台API 完整城市列表简单方式

    activity_main.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android& ...

随机推荐

  1. STL 函数适配器(function adapter)

    函数适配器(function adapter):通过不同函数适配器的绑定,组合和修饰能力,可以实现强大的功能,配合STL泛型算法完成复杂功能. 绑定(bind) template <class ...

  2. [MicroSoft]Introducing .NET 5

    Introducing .NET 5 Richard https://devblogs.microsoft.com/dotnet/introducing-net-5/ 将路线图 完整的给出来了. Ma ...

  3. Julia出现错误ERROR: LoadError: syntax: try without catch or finally

    因项目要求进行机器学习数据可视化,要求尝试使用Julia,在此,记录下遇到的坑,仅为记录效果.后续陆续更新. 问题一:关于LightML库中的坑:ERROR: LoadError: syntax: t ...

  4. 最少多少人说谎(dp)

    https://ac.nowcoder.com/acm/contest/1168/H 题意:n个学生,邓志聪想知道这些学生的考试情况,于是一个一个叫这些学生叫去办公室问他们,但是有些学生并没有讲真话, ...

  5. Serilog

    参考 asp.net core使用serilog将日志推送到腾讯云日志服务

  6. gomock

    参考 使用Golang的官方mock工具--gomock

  7. 线性渐变css

    从上到下的线性渐变: #grad { background: -webkit-linear-gradient(red, blue); /* Safari 5.1 - 6.0 */ background ...

  8. Spring基础08——使用p命名空间

    1.使用p命名空间 为了简化xml配置,越来越多的xml文件采用属性而非子元素配置信息,Spring从2.5版本开始引入了一个新的p命名空间,可以通过<bean>元素属性的方式配置Bean ...

  9. Educational Codeforces Round 32 Almost Identity Permutations CodeForces - 888D (组合数学)

    A permutation p of size n is an array such that every integer from 1 to n occurs exactly once in thi ...

  10. Codeforces Round #569 (Div. 2) 题解A - Alex and a Rhombus+B - Nick and Array+C - Valeriy and Dequ+D - Tolik and His Uncle

    A. Alex and a Rhombus time limit per test1 second memory limit per test256 megabytes inputstandard i ...