Android 联系人A-Z列表布局实现分析。

本文介绍了如何实现Android应用中联系人列表的A-Z布局。实现思路包括创建右边的字母导航条,左边根据导航自动滚动到相应字母的联系人,并提供了自定义A-ZView的详细步骤,包括隐藏字母视图、设置监听器接口。此外,文章还提到了使用pinyin4j库进行中文转拼音的方法。

一,首先看效果图


二,实现思路

1,右边的A-Z导航。

2,左边的A-Z字母的显示。

3,点击右边的,左边自动跟踪字母。


1,自定义A-ZView

/**
 * 执行顺序 onSizeChanged()--->onDraw()
 */
public class A_ZindexView extends View {
	private Paint paint;
	private OnSlideTouchListener listener;
	// 单元格的高度
	private float cellHeigth;
	private float cellwidth;

	private static String[] strs = new String[] { "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" };

	private int touchIndex = -1;

	public A_ZindexView(Context context) {
		super(context);
		init();
	}

	public A_ZindexView(Context context, AttributeSet attrs) {
		super(context, attrs);
		init();
	}

	public A_ZindexView(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		init();
	}

	private void init() {
		paint = new Paint();
		paint.setColor(Color.WHITE);
		paint.setTypeface(Typeface.DEFAULT_BOLD);

	}

	/* 在这个方法中测量控件的宽高 */
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		// 得到每个单元格的高度
		cellHeigth = getHeight() / strs.length;
		// 得到单元格的宽度
		cellwidth = getMeasuredWidth();
	}
	
	@Override
	protected void onDraw(Canvas canvas) {
		// 绘制A-Z.

		for (int i = 0; i < strs.length; i++) {

			// 获取到X轴的坐标
			int x = (int) (cellwidth / 2 - paint.measureText(strs[i]) / 2);

			// 获取到文字的高度
			Rect bounds = new Rect();

			paint.getTextBounds(strs[i], 0, strs[i].length(), bounds);
			float textHeigth = bounds.height();

			// 获取到Y轴的坐标
			int y = (int) (cellHeigth / 2 + textHeigth / 2 + i * cellHeigth);

			paint.setColor(touchIndex == i ? Color.GRAY : Color.WHITE);

			canvas.drawText(strs[i], x, y, paint);
		}
		super.onDraw(canvas);

	}
	
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (MotionEventCompat.getActionMasked(event)) {

		case MotionEvent.ACTION_UP:
			touchIndex = -1;
			break;

		default:
			// 手指触摸的高度
			float f = event.getY();
			int index = (int) (f / cellHeigth);
			if (index >= 0 && index < strs.length) {
				if (touchIndex != index) {
					System.out.println(strs[index]);
					listener.onBack(strs[index]);
					touchIndex = index;
				}
			}
			break;
		}
		//刷新
		invalidate();
		return true;
	}
	
	/**
	 * 滑动的回调接口
	 * 
	 * @author Administrator
	 * 
	 */
	public interface OnSlideTouchListener {
		void onBack(String str);
		
	}

	public void setOnSlideTouchListener(OnSlideTouchListener listener) {
		this.listener = listener;
	}

}

2,左边的A-Z字母的显示。

     ilistView 里面的item 的布局是 一个字母TextView,一个名字View 。  当存在时 隐藏字母View。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tv_py"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:background="#888888"
        android:gravity="center_vertical"
        android:paddingLeft="20dp"
        android:text="A"
        android:visibility="gone"
        android:textColor="@android:color/white" />

    <TextView
        android:id="@+id/tv_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="center_vertical"
        android:paddingLeft="20dp"
        android:text="小大些" />

</LinearLayout>

3,点击右边的,左边自动跟踪字母。

  定义一个接口 设置监听器, 自定义View上面已经写了。


4,里面需要一个中文转化拼音的工具。

 用的是pinyin4j-2.5.0.jar,作为第三方jar引入工程。

然后写一个工具类调用它的方法。

/**
 * 将汉字转换为拼音
 * 
 * @author xiaozhifeng
 * 
 */
public class PinYinUtils {

	public static String getPinyin(String string) {
		HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
		// 设置汉语拼音为大写
		format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
		// 设置汉语拼音不显示声调
		format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);

		// 将传入的汉字转化为char数组
		char[] cs = string.toCharArray();
		// 记录每一个字符
		String s = "";
		// 拼接所有的字符
		StringBuffer buffer = new StringBuffer();
		// 遍历所有的字符
		for (int i = 0; i < cs.length; i++) {

			char c = cs[i];
			// 检查是否是空格
			if (Character.isWhitespace(c)) {
				continue;
			}

			if (c > -127 && c < 128) {
				buffer.append(c);
			} else {
				try {
					s = PinyinHelper.toHanyuPinyinStringArray(c, format)[0];
					buffer.append(s);
				} catch (Exception e) {
					e.printStackTrace();
					buffer.append(s);
				}
			}

		}

		return buffer.toString();

	}
}


demo下载:http://download.csdn.net/detail/androidforme/9237531 



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值