在开发app的过程中,如果用到通讯录或者类似的列表,需要快速在其中定位,可以根据列表项的拼音首字母来定位,这时候就需要用到右侧字母索引了。必如现在的微信通讯录界面就是如此。在实现这种功能的过程中,还是挺复杂的,很难我觉得。在网上各种查找资料,困难重重,好在最后终于捯饬出来了,伤不起。。。。特此记录一下写的过程。
1、创建自定的view,用作右侧列表索引。
-
public class RulerWidget extends View {
-
-
public static String[] indexStr = {
-
"#", "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 static int INDEX_LENGTH = indexStr.length;
-
-
OnTouchingLetterChangedListener onTouchingLetterChangedListener;
-
Paint mPaint = new Paint();
-
boolean showBkg = false;
-
int choose = -1;
-
-
public RulerWidget(Context context) {
-
super(context);
-
}
-
-
public RulerWidget(Context context, AttributeSet attrs, int defStyle) {
-
super(context, attrs, defStyle);
-
}
-
-
public RulerWidget(Context context, AttributeSet attrs) {
-
super(context, attrs);
-
}
-
-
@Override
-
protected void onDraw(Canvas canvas) {
-
super.onDraw(canvas);
-
-
canvas.drawColor(Color.parseColor("#40000000"));
-
-
-
int height = getHeight();
-
int width = getWidth();
-
int singleHeight = height / indexStr.length;
-
for(int i=0;i<indexStr.length;i++){
-
mPaint.setColor(Color.WHITE);
-
mPaint.setTextSize(24);
-
mPaint.setTypeface(Typeface.DEFAULT_BOLD);
-
mPaint.setAntiAlias(true);
-
if(i == choose){
-
mPaint.setColor(Color.parseColor("#3399ff"));
-
mPaint.setFakeBoldText(true);
-
}
-
float xPos = width/2 - mPaint.measureText(indexStr[i])/2;
-
float yPos = singleHeight * i + singleHeight;
-
canvas.drawText(indexStr[i], xPos, yPos, mPaint);
-
mPaint.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()*indexStr.length);
-
-
switch (action) {
-
case MotionEvent.ACTION_DOWN:
-
showBkg = true;
-
if(oldChoose != c && listener != null){
-
if(c > 0 && c< indexStr.length){
-
listener.onTouchingLetterChanged(indexStr[c]);
-
choose = c;
-
invalidate();
-
}
-
}
-
-
break;
-
case MotionEvent.ACTION_MOVE:
-
if(oldChoose != c && listener != null){
-
if(c > 0 && c< indexStr.length){
-
listener.onTouchingLetterChanged(indexStr[c]);
-
choose = c;
-
invalidate();
-
}
-
}
-
break;
-
case MotionEvent.ACTION_UP:
-
-
choose = -1;
-
invalidate();
-
break;
-
}
-
return true;
-
}
-
-
@Override
-
public boolean onTouchEvent(MotionEvent event) {
-
return super.onTouchEvent(event);
-
}
-
-
public void setOnTouchingLetterChangedListener(
-
OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
-
this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
-
}
-
-
}
对应的回调接口定义:
-
-
-
-
-
public interface OnTouchingLetterChangedListener{
-
public void onTouchingLetterChanged(String s);
-
}
2、创建fragment片段对应的布局文件:
-
<?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" >
-
-
<FrameLayout
-
android:layout_width="match_parent"
-
android:layout_height="match_parent"
-
android:background="#ffffff" >
-
-
<ListView
-
android:id="@+id/listView"
-
android:layout_width="match_parent"
-
android:layout_height="wrap_content"
-
android:cacheColorHint="#00000000"
-
android:fadingEdge="none"
-
android:scrollbars="none" >
-
-
</ListView>
-
-
<TextView
-
android:id="@+id/tv"
-
android:layout_width="60dp"
-
android:layout_height="60dp"
-
android:gravity="center"
-
android:background="#f0606060"
-
android:layout_gravity="center"
-
android:text="A"
-
android:textColor="#ffffff"
-
android:textSize="30sp" />
-
-
<com.hy.ticket.view.RulerWidget
-
android:id="@+id/sidrbar"
-
android:layout_width="30.0dip"
-
android:layout_height="fill_parent"
-
android:layout_gravity="right|center" />
-
</FrameLayout>
-
-
</LinearLayout>
3、引入pinyin4j.jar包(版本建议使用最新),写一个工具类,包含获取中文字符串拼音首字母,获取中文拼音等方法。
-
public class StringHelper {
-
-
public static String getPingYin(String src) {
-
char[] t = src.toCharArray();
-
String[] strs = new String[t.length];
-
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
-
format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
-
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
-
format.setVCharType(HanyuPinyinVCharType.WITH_V);
-
String str = "";
-
try{
-
for(int i=0; i<t.length; i++) {
-
if(java.lang.Character.toString(t[i]).matches(
-
"[\u4E00-\u9FA5]+")) {
-
strs = PinyinHelper.toHanyuPinyinStringArray(t[i], format);
-
str += strs[0];
-
}else{
-
str += java.lang.Character.toString(t[i]);
-
}
-
}
-
-
发表于 2020-03-08 10:28:15
- 阅读 ( 1300 )
- 分类: