侧边字母索引栏的使用


侧边字母索引栏的使用

 

 

自定义一个view 绘制

package com.lolo.my361.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;

public class LetterlistViewForFriend extends View {

	public LetterlistViewForFriend(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		// TODO Auto-generated constructor stub
	}
	public LetterlistViewForFriend(Context context)
	{
		super(context);
	}

	public LetterlistViewForFriend(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
	}

	OnTouchingLetterChangedListener onTouchingLetterChangedListener;
	String[] b =
	{"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" };
	int choose = -1;
	Paint paint = new Paint();
	
	boolean showBkg = false;

	@Override
	protected void onDraw(Canvas canvas)
	{
		super.onDraw(canvas);
		
		if (showBkg)
		{
			canvas.drawColor(Color.parseColor("#40000000"));
		}
		
		int height = getHeight();
		int width = getWidth();
		int singleHeight = height / b.length;
		//循环绘制字母
		for (int i = 0; i < b.length; i++)
		{
			//paint.setTextSize(19) ;
			paint.setColor(Color.BLACK);
			paint.setTypeface(Typeface.DEFAULT_BOLD);
			
			paint.setAntiAlias(true);
			if (i == choose)
			{
				//被选中了改变文字颜色
				paint.setColor(Color.parseColor("#3399ff"));
				paint.setFakeBoldText(true);
			}
			float xPos = width / 2 - paint.measureText(b[i]) / 2;
			float yPos = singleHeight * i + singleHeight;
			
			canvas.drawText(b[i], xPos, yPos, paint);
			paint.reset();
		}

	}

	@Override
	public boolean dispatchTouchEvent(MotionEvent event)
	{
		final int action = event.getAction();
		final float y = event.getY();
		final int oldChoose = choose;
		final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
		//计算出第几个
		final int c = (int) (y / getHeight() * b.length);

		switch (action)
		{
		case MotionEvent.ACTION_DOWN:
			showBkg = true;
			if (oldChoose != c && listener != null)
			{
				if (c > 0 && c < b.length)
				{
					listener.onTouchingLetterChanged(b[c]);
					choose = c;
					invalidate();
				}
			}

			break;
		case MotionEvent.ACTION_MOVE:
			if (oldChoose != c && listener != null)
			{
				if (c > 0 && c < b.length)
				{
					listener.onTouchingLetterChanged(b[c]);
					choose = c;
					invalidate();
				}
			}
			break;
		case MotionEvent.ACTION_UP:
			showBkg = true;
			if (oldChoose != c && listener != null)
			{
				if (c > 0 && c < b.length)
				{
					//回调
					listener.onTouchingLetterChanged(b[c]);
					choose = c;
					invalidate();
				}
			}
			
			break;
		}
		return true;
	}

	
	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		return super.onTouchEvent(event);
	}
	//方便Activity类调用,用于显示
	public void setOnTouchingLetterChangedListener(OnTouchingLetterChangedListener onTouchingLetterChangedListener)
	{
		this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
	}

	public interface OnTouchingLetterChangedListener
	{
		public void onTouchingLetterChanged(String s);
	}
}

 
 

	// 初始化汉语拼音首字母弹出提示框
	private void initOverlay() {
		LayoutInflater inflater = LayoutInflater.from(this);
		overlay = (TextView) inflater.inflate(R.layout.overlay_layout, null);
		overlay.setVisibility(View.INVISIBLE);
		WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
				LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,
				WindowManager.LayoutParams.TYPE_APPLICATION,
				WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
						| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,
				PixelFormat.TRANSLUCENT);
		WindowManager windowManager = (WindowManager) this
				.getSystemService(Context.WINDOW_SERVICE);
		windowManager.addView(overlay, lp);
	}

 触摸的时候显示字母,且ListView自动滚动该字母列

letterlistViewForFriend.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
			
			@Override
			public void onTouchingLetterChanged(String s) {
				// TODO Auto-generated method stub
				System.out.println(s);
				int position = alphaIndex.get(s);
				//listView 滚动到选择的字母那个地方。
				if (alphaIndex.get(s) != null) {
					lv_friend.setSelection(position);
				}
				overlay.setText(s);
				overlay.setVisibility(View.VISIBLE);
				handler.removeCallbacks(overlayThread);
				// 延迟一秒后执行,让overlay为不可见
				handler.postDelayed(overlayThread, 1500);
			}
		});