最近闲的很,没什么事干 ,在玩手机的时间看到android系统自带的那个通讯录软件对联系人的快速定位功能.  感觉这个功能也比较实用自己就试着自己去实现.

虽然网络上还是有大牛封闭好了的框架,但是如果自己来实现一下也是不错的, 个人比较喜欢自己写的东西,别人写好的东西可以拿来借鉴,还是不推荐看也不看直接拿

来用,代码可以复制,作者的思想就需要慢慢体会的.

基本介绍:

   首先安卓本身已经提供一个接口来实现快速定位的, SectionIndexer接口共有三个方法.

   

    Object[] getSections();              //返回所有的section

    

    int getPositionForSection(int sectionIndex);    //根据section索引返回一个position

   int getSectionForPosition(int position);      //与上面的方法正好相反 .

   section可以理解为一个ListView中的一部分,比如在联系人进行分组将首字母相同的分为同一组,每一组就是一个section.

基本设计:

  我将那些字母的列表看成是一个View这个View里面包含一个实现SectionIndexer接口的成员. 且定义一个回调接口用于在索引更改时通知更新ListView.

  重写onMeasure(int,int)方法计算View的宽高.

  

	@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { if(mSectionIndex == null){
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
return ;
} if(mSections == null)
mSections = mSectionIndex.getSections(); int measureHeight ;
int measureWidth ;
int height = (int) (sectionHeight() * mSections.length ) + ( getPaddingTop() + getPaddingBottom() );
int width = sectionWidth() + getPaddingLeft() + getPaddingRight(); /**
* 根据布局参数来设置View的宽高.
* 如果布局参数的高或宽为LayoutParams.WRAP_CONTENT
* 则View的宽高分别为 width , height
* 否则直接根据布局参数的数值来设置
*/
LayoutParams lp = getLayoutParams();
if(lp.height != LayoutParams.WRAP_CONTENT)
height = lp.height; if(lp.width != LayoutParams.WRAP_CONTENT)
width = lp.width; /** */
measureHeight = ViewGroup.getChildMeasureSpec(heightMeasureSpec, 0,height);
measureWidth = ViewGroup.getChildMeasureSpec(widthMeasureSpec, 0, width); setMeasuredDimension(measureWidth, measureHeight);
}

重写onLayout方法,重写该方法的原因是使所有的索引填满View. 不一定要重写onLayout方法,只要在View能够得到高度后再计算就可以.

@Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
/**
* view 的高度大于列表显示的高度, 在每一个字母之间加入一些间隔,
* 使每一个字母对齐,并填满整个view.
*/
int viewHeight = getHeight() - (getPaddingTop() + getPaddingBottom());
int originalHeight = mHeight * mSections.length; int overHeight = viewHeight - originalHeight; if(overHeight <= 0) return ; mAlphaInterval = overHeight / (mSections.length);
}

重写onDraw方法,这个方法就不用多说了吧,大家都知道是干什么的,直接上代码.

	@Override
protected void onDraw(Canvas canvas) { if(mSectionIndex == null)
return ; int height = getHeight();
int widht = getWidth();
//画背景
if(mBackground){
RectF round = new RectF(0, 0, widht, height);
canvas.drawRect(round, mBackgroundPaint);
} //画字母
float textheight = mAlphaPaint.descent()- mAlphaPaint.ascent();
float y = textheight / 1.5f + getPaddingTop(); //第一个字母偏移 .
float x = getPaddingLeft() ;
for(int i = 0; i < mSections.length ; i++){ if(mCurrentSection == i)
mAlphaPaint.setColor(Color.BLUE);
else
mAlphaPaint.setColor(Color.WHITE); y += mAlphaPadding + mAlphaInterval;
canvas.drawText(mSections[i].toString() , x, y, mAlphaPaint);
y += mAlphaPadding + textheight ;
} }

效果图:

  

最后还包含一些辅助方法,就不一一例举的大家下载源码一看便知.

源码下载 :

  http://pan.baidu.com/s/1gdw1gyf

android字母索引实现ListView定位的更多相关文章

  1. 做个简单的Android列表字母索引控件

    相信大家在许多App中都见到过带字母索引的界面,比如我最近看到的这个开源控件: WaveSideBar 很酷是不是?!!!如果加在例如联系人列表界面上,大大提升了用户体验. 那么这个索引控件要怎么做呢 ...

  2. 老猪带你玩转android自定义控件二——自定义索引栏listview

    带索引栏的listview,在android开发非常普遍,方便用户进行字母索引,就像微信通讯录这样: 今天,我们就从零到一实现这个具有索引栏的listview. 怎么实现这个控件了,我们应当梳理出一个 ...

  3. 玩转android自定义控件二——自定义索引栏listview

    带索引栏的listview,在android开发非常普遍,方便用户进行字母索引,就像微信通讯录这样: 今天,我们就从零到一实现这个具有索引栏的listview. 怎么实现这个控件了,我们应当梳理出一个 ...

  4. 联系人的侧边字母索引ListView 将手机通讯录姓名通过首字母排序。

      package com.lixu.letterlistview; import java.util.ArrayList; import java.util.List; import org.apa ...

  5. android之listView定位到指定行同时隐藏输入键盘

    帮别人该bug遇到的一个问题,记录下来. listView.setSelection(a); 这个方法可以让让你的listview定位到指定行 但是如果紧接着执行隐藏输入键盘的代码,则会有bug,这个 ...

  6. android之listView定位到指定行同一时候隐藏输入键盘

    帮别人该bug遇到的一个问题,记录下来. listView.setSelection(a); 这种方法能够让让你的listview定位到指定行 可是假设紧接着运行隐藏输入键盘的代码.则会有bug.这个 ...

  7. 8.快速索引、listview

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

  8. Android带索引联系人列表

    网上Android联系人列表的样例也非常多,都和微信的联系人差点儿相同,因为项目用到了联系人列表索引功能(产品把字母item给去掉了),只是也还是好实现.这里我也来分享分享我的实现,免得以后忘了.那先 ...

  9. Android中动态更新ListView(转)

    在使用ListView时,会遇到当ListView列表滑动到最底端时,添加新的列表项的问题,本文通过代码演示如何动态的添加新的列表项到ListView中.实现步骤:调用ListView的setOnSc ...

随机推荐

  1. 12_goto语句的使用

    1.goto是一个关键字,其作用是运行到goto语句进行跳转,立即执行goto后面所对应标签的语句2.结构:goto 标签名(任意起)3.goto语句尽量不要跨函数使用,否则会使代码看起来非常乱,可读 ...

  2. appium在windows系统下环境搭建

    对于appium的介绍我就不说了,之前的文章介绍过.下面直入主题. 命令版本在安装过程中需要有python2环境,装完你可以装python3来写脚本. 环境要求: JDK >java语言安装包 ...

  3. SPSS数据分析方法不知道如何选择

      一提到数学,高等数学,线性代数,概率论与数理统计,数值分析,空间解析几何这些数学课程,头疼呀.作为文科生,遇见这些课程时,通常都是各种寻求帮助,班上有位宅男数学很厉害,各种被女生‘围观’,这数学为 ...

  4. maven构建springmvc过程中遇到的一些问题

    1.spring的配置文件,xsd不要配版本号 2.spring配置datasource的三种方式 3.springmvc与spring扫描注解的冲突 4.NoClassDefFoundError: ...

  5. 2019DX#3

    Solved Pro.ID Title Ratio(Accepted / Submitted)   1001 Azshara's deep sea 凸包 6.67%(6/90)  

  6. CodeForces 223C Partial Sums 多次前缀和

    Partial Sums 题解: 一个数列多次前缀和之后, 对于第i个数来说他的答案就是 ; i <= n; ++i){ ; j <= i; ++j){ b[i] = (b[i] + 1l ...

  7. CF 990D Graph And Its Complement 第十八 构造、思维

    Graph And Its Complement time limit per test 2 seconds memory limit per test 256 megabytes input sta ...

  8. 2015北京区域赛 Xiongnu's Land

    Wei Qing (died 106 BC) was a military general of the Western Han dynasty whose campaigns against the ...

  9. adb命令介绍

    1.adb logcat -v time -s ActivityManager:I 获取包名和activity 2. adb logcat "ActivityManager" |g ...

  10. Django之FBV和CBV的用法

    FBV FBV,即 func base views,函数视图,在视图里使用函数处理请求. 以用户注册代码为例, 使用两个函数完成注册 初级注册代码 def register(request): &qu ...