上一篇文章虽然实现了ListView 快速索引的效果,但是有一个小小的Bug。这个Bug我在前面也说了,这篇文章就来解决这个Bug。

我研究的时候发现只要showBg值为true,中间的字母就显示,而当showBg 的值为false的时候中间的字母就可以消失。只要SlideBar的状态为ACTION_DOWN和ACTION_MOVE 的时候showBg的值为true,而ACTION_UP的时候showBg的值就为false;

所以根据上面这个特征,我们只要把OnToucheLetterChange()这个回调函数的参数改一下就可以了。改成onTouchLetterChange(boolean isTouched, String s)

boolean类型的参数直接把showBg传过去就可以了。

改进后的代码如下:

package com.folyd.tuan.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View; /**
* 自定义的View,实现ListView A~Z快速索引效果
*
* @author Folyd
*
*/
public class SlideBar extends View {
private Paint paint = new Paint();
private OnTouchLetterChangeListenner listenner;
// 是否画出背景
private boolean showBg = false;
// 选中的项
private int choose = -1;
// 准备好的A~Z的字母数组
public static String[] letters = { "#", "A", "B", "C", "D", "E", "F", "G",
"H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y", "Z" }; // 构造方法
public SlideBar(Context context) {
super(context);
} public SlideBar(Context context, AttributeSet attrs) {
super(context, attrs);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
// 获取宽和高
int width = getWidth();
int height = getHeight();
// 每个字母的高度
int singleHeight = height / letters.length;
if (showBg) {
// 画出背景
canvas.drawColor(Color.parseColor("#55000000"));
}
// 画字母
for (int i = 0; i < letters.length; i++) {
paint.setColor(Color.BLACK);
// 设置字体格式
paint.setTypeface(Typeface.DEFAULT_BOLD);
paint.setAntiAlias(true);
paint.setTextSize(20f);
// 如果这一项被选中,则换一种颜色画
if (i == choose) {
paint.setColor(Color.parseColor("#F88701"));
paint.setFakeBoldText(true);
}
// 要画的字母的x,y坐标
float posX = width / 2 - paint.measureText(letters[i]) / 2;
float posY = i * singleHeight + singleHeight;
// 画出字母
canvas.drawText(letters[i], posX, posY, paint);
// 重新设置画笔
paint.reset();
}
} /**
* 处理SlideBar的状态
*/
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
final float y = event.getY();
// 算出点击的字母的索引
final int index = (int) (y / getHeight() * letters.length);
// 保存上次点击的字母的索引到oldChoose
final int oldChoose = choose;
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
showBg = true;
if (oldChoose != index && listenner != null && index > 0
&& index < letters.length) {
choose = index;
listenner.onTouchLetterChange(showBg, letters[index]);
invalidate();
}
break; case MotionEvent.ACTION_MOVE:
if (oldChoose != index && listenner != null && index > 0
&& index < letters.length) {
choose = index;
listenner.onTouchLetterChange(showBg, letters[index]);
invalidate();
}
break;
case MotionEvent.ACTION_UP:
showBg = false;
choose = -1;
if (listenner != null) {
if (index <= 0) {
listenner.onTouchLetterChange(showBg, "A");
} else if (index > 0 && index < letters.length) {
listenner.onTouchLetterChange(showBg, letters[index]);
} else if (index >= letters.length) {
listenner.onTouchLetterChange(showBg, "Z");
}
}
invalidate();
break;
}
return true;
} /**
* 回调方法,注册监听器
*
* @param listenner
*/
public void setOnTouchLetterChangeListenner(
OnTouchLetterChangeListenner listenner) {
this.listenner = listenner;
} /**
* SlideBar 的监听器接口
*
* @author Folyd
*
*/
public interface OnTouchLetterChangeListenner { void onTouchLetterChange(boolean isTouched, String s);
} }

Activity中就很容易处理了:

mSlideBar
.setOnTouchLetterChangeListenner(new OnTouchLetterChangeListenner() { @Override
public void onTouchLetterChange(boolean isTouched, String s) { float_letter.setText(s);
if (isTouched) {
float_letter.setVisibility(View.VISIBLE);
} else {
float_letter.postDelayed(new Runnable() { @Override
public void run() {
float_letter.setVisibility(View.GONE);
}
}, 100);
}
int position = array.indexOf(s);
mListView.setSelection(position);
}
});

这样就解决Bug了。哈哈。

Android ListView A~Z快速索引(改进版)的更多相关文章

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

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

  2. 8.快速索引、listview

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

  3. Android 快速索引(城市列表和联系人)

    最近需要实现一个城市列表的快速索引功能.类似于联系人应用,根据姓名首字母快速索引功能. 要实现这个功能只需要解决两个问题:1.对列表进行分组(具有同一特征),并且能够快速定位到该组的第一项 2.右侧分 ...

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

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

  5. [Android分享] 【转帖】Android ListView的A-Z字母排序和过滤搜索功能

      感谢eoe社区的分享   最近看关于Android实现ListView的功能问题,一直都是小伙伴们关心探讨的Android开发问题之一,今天看到有关ListView实现A-Z字母排序和过滤搜索功能 ...

  6. android ListView 九大重要属性详细分析、

    android ListView 九大重要属性详细分析. 1.android ListView 一些重要属性详解,兄弟朋友可以参考一下. 首先是stackFromBottom属性,这只该属性之后你做好 ...

  7. 【腾讯Bugly干货分享】Android ListView与RecyclerView对比浅析--缓存机制

    本文来自于腾讯bugly开发者社区,非经作者同意,请勿转载,原文地址:http://dev.qq.com/topic/5811d3e3ab10c62013697408 作者:黄宁源 一,背景 Recy ...

  8. 【转】android ListView 几个重要属性

    android ListView 几个重要属性 分类: Android2012-03-08 19:25 19324人阅读 评论(5) 收藏 举报 listviewandroid活动javalistnu ...

  9. 实现Android ListView 自动加载更多内容

    研究了几个小时终于实现了Android ListView 自动加载的效果. 说说我是怎样实现的.分享给大家. 1.给ListView增加一个FooterView,调用addFooterView(foo ...

随机推荐

  1. 英文Ubuntu下Emacs 使用 ibus 五笔

    Ubuntu 12.04 Precise 英文版本 1. 安装ibus/安装ibus-table-wubi http://wiki.ubuntu.org.cn/IBus 其中im-switch命令要在 ...

  2. C# Assembly类_反射

    System.Reflection.Assembly类是一个比较常用到的类,在反射中就经常用到. 由于这个类实在有太多的字段.属性与方法.实在不想将一个个属性方法从MSDN复制出来然后逐个属性.方法敲 ...

  3. SQL数据转移

    方法一:如果想把数据库A中的表Table1中的数据复制到数据库B中的表Table2中,也就是要预先建立Table2,可以使用一下语句: use B goinsert into Table2 selec ...

  4. 【Shell脚本】运行shell脚本文件的几种方法与区别

    Shell脚本不同的运行方式会对当前Shell设置或者运行结果有所不同. 假设现在有一个脚本名为display_shell_script_args.sh,其内容如下: #!/home/pyf/bin/ ...

  5. 使用univocity-parsers创建和读取csv文件

    import com.univocity.parsers.csv.CsvFormat;import com.univocity.parsers.csv.CsvParser;import com.uni ...

  6. vim中对文本的选择

    本文主要解说vim中对文本的选择,vim中选择文本分为: (1)选择字符  ----  命令行模式下输入小写v (2)选择行     ----  命令行模式下输入大写V (3)选择块     ---- ...

  7. Paxos算法深入分析

    在分布式系统设计领域,Paxos可谓是最重要一致性的算法.Google的大牛们称   All working protocols for asynchronous consensus we have ...

  8. 【Java基础】几种简单的调用关系与方法

    直接上代码吧. class lesson4AB //同一个类下的public修饰的方法A,B可以相互调用 { public void A() { B();//等价于this.B(); } public ...

  9. W3C-XML

    XML XML Extensible Markup Language,可扩展标记语言 1 XML和HTML的区别 XML主要用来传输数据 HTML主要用来呈现数据内容 2 XML的主要用途 传输数据 ...

  10. 网页被Chrome识别成英语,区域,语言,网站

    修改成这个后解决 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...