类似于Android中通讯录的实现
开发中难免会遇到类似于通讯录的开发,做出来供大家参考。
实现图
图一为点击右方的字母实现图,链表会到F的那个条目
图二为效果图
具体代码
这里需要导入一个 pinyin4j-2.5.0.jar 包是将汉字转为拼音的jar包和一个 nineoldandroids-2.4.0.jar包这是我的代码中用的一个动画效果
首先主页面
QuickIndexBarActivity.java
package com.example.custom_view;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.Window;
import android.widget.ListView;
import android.widget.TextView;
import com.nineoldandroids.view.ViewHelper;
import com.nineoldandroids.view.ViewPropertyAnimator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class QuickIndexBarActivity extends AppCompatActivity {
private MyQuickIndexBar mQuickIndexBar;
private ListView mLv;
private List list = new ArrayList();
private TextView mTv_window;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_quick_index_bar);
initView();
}
private void initView() {
mLv = findViewById(R.id.lv);
mTv_window = findViewById(R.id.tv_window);
mQuickIndexBar = findViewById(R.id.my_quickIndexbar);
list = getStrList();
Collections.sort(list);
MyAdapter adapter = new MyAdapter(QuickIndexBarActivity.this, list);
mLv.setAdapter(adapter);
mQuickIndexBar.setOnItemtClickListenner(new MyQuickIndexBar.onItemClickListenner() {
@Override
public void getOnImtemPosition(String letter) {
for (int i = 0; i < list.size(); i++) {
String strLetter = list.get(i).getmPinYinName().charAt(0) + "";
if (letter.equalsIgnoreCase(strLetter)) {
mLv.setSelection(i);
break;
}
}
showCurrentTvWindow(letter);
}
});
ViewHelper.setScaleX(mTv_window, 0F);
ViewHelper.setScaleY(mTv_window, 0F);
}
private Handler mHandler = new Handler();
private boolean mFlag;
private void showCurrentTvWindow(String letter) {
if (!mFlag) {
mFlag = true;
ViewPropertyAnimator.animate(mTv_window).setDuration(450).scaleX(1.0F).start();
ViewPropertyAnimator.animate(mTv_window).setDuration(450).scaleY(1.0F).start();
}
mTv_window.setText(letter);
mHandler.removeCallbacksAndMessages(null);
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mFlag = false;
ViewPropertyAnimator.animate(mTv_window).setDuration(450).scaleX(0.0F).start();
ViewPropertyAnimator.animate(mTv_window).setDuration(450).scaleY(0.0F).start();
}
}, 2000);
}
public List getStrList() {
list.add(new PinYinInfo("北京"));
list.add(new PinYinInfo("深圳"));
list.add(new PinYinInfo("广州"));
list.add(new PinYinInfo("天津"));
list.add(new PinYinInfo("河北"));
list.add(new PinYinInfo("河南"));
list.add(new PinYinInfo("山西"));
list.add(new PinYinInfo("湖北"));
list.add(new PinYinInfo("湖南"));
list.add(new PinYinInfo("辽宁"));
list.add(new PinYinInfo("上海"));
list.add(new PinYinInfo("宁夏"));
list.add(new PinYinInfo("内蒙古"));
list.add(new PinYinInfo("重庆"));
list.add(new PinYinInfo("四川"));
list.add(new PinYinInfo("贵州"));
list.add(new PinYinInfo("吉林"));
list.add(new PinYinInfo("广西"));
list.add(new PinYinInfo("陕西"));
list.add(new PinYinInfo("新疆"));
list.add(new PinYinInfo("黑龙江"));
list.add(new PinYinInfo("福建"));
list.add(new PinYinInfo("江苏"));
list.add(new PinYinInfo("江西"));
list.add(new PinYinInfo("安徽"));
list.add(new PinYinInfo("大连"));
list.add(new PinYinInfo("鄂尔多斯"));
list.add(new PinYinInfo("东莞"));
list.add(new PinYinInfo("沧州"));
list.add(new PinYinInfo("长春"));
list.add(new PinYinInfo("成都"));
list.add(new PinYinInfo("海南"));
list.add(new PinYinInfo("香港"));
list.add(new PinYinInfo("北小营"));
list.add(new PinYinInfo("台湾"));
list.add(new PinYinInfo("保定"));
list.add(new PinYinInfo("包头"));
list.add(new PinYinInfo("北海"));
list.add(new PinYinInfo("澳门"));
list.add(new PinYinInfo("安阳"));
list.add(new PinYinInfo("鞍山"));
list.add(new PinYinInfo("西藏"));
list.add(new PinYinInfo("安庆"));
list.add(new PinYinInfo("浙江"));
list.add(new PinYinInfo("云南"));
list.add(new PinYinInfo("甘肃"));
list.add(new PinYinInfo("山东"));
return list;
}
}
activity_quick_index_bar.class
布局中MyQuickIndexBar会报错,这就得看你创的项目名了,自己根据情况修改。
MyQuickIndexBar.java
package com.example.custom_view;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
public class MyQuickIndexBar extends View {
private String[] mStrArr = {"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 MyQuickIndexBar(Context context) {
this(context, null);
}
public MyQuickIndexBar(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public MyQuickIndexBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
private int mWidth;
private float mCellHeight;
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
mWidth = getMeasuredWidth();
mCellHeight = getMeasuredHeight() * 1F / mStrArr.length;
}
private Paint mPaint;
private void init() {
mPaint = new Paint();
mPaint.setAntiAlias(true);
mPaint.setColor(Color.BLACK);
//以文本的中心线为起始点开始绘制文本
mPaint.setTextAlign(Paint.Align.CENTER);
mPaint.setTextSize(50);
}
@Override
protected void onDraw(Canvas canvas) {
for (int i = 0; i < mStrArr.length; i++) {
float x = mWidth / 2;
float y = mCellHeight / 2 + getTextHeight(mStrArr[i]) / 2 + i * mCellHeight + 10;
mPaint.setColor(mLastIndex == i ? Color.parseColor("#FFFF00") : Color.BLACK);
canvas.drawText(mStrArr[i], x, y, mPaint);
}
super.onDraw(canvas);
}
private float getTextHeight(String text) {
Rect rect = new Rect();
mPaint.getTextBounds(text, 0, text.length(), rect);
return rect.bottom;
}
private int mLastIndex = -1;
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
int index = (int) (event.getY() / mCellHeight);
if (index>-1&&index
MyAdapter.java
package com.example.custom_view;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List mList;
public MyAdapter(Context context, List strList) {
this.mContext = context;
this.mList = strList;
}
@Override
public int getCount() {
return mList.size();
}
@Override
public Object getItem(int position) {
return mList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null)
convertView = View.inflate(mContext, R.layout.list_item, null);
ViewHolder viewHolder = ViewHolder.getViewHolder(convertView);
String currentStrLetter = mList.get(position).getmPinYinName().charAt(0) + "";
if (position > 0) {
String lastStrLetter = mList.get(position - 1).getmPinYinName().charAt(0) + "";
if (lastStrLetter.equals(currentStrLetter)) {
viewHolder.tv_letter.setVisibility(View.GONE);
} else {
viewHolder.tv_letter.setText(currentStrLetter);
viewHolder.tv_letter.setVisibility(View.VISIBLE);
}
} else {
viewHolder.tv_letter.setText(currentStrLetter);
viewHolder.tv_letter.setVisibility(View.VISIBLE);
}
viewHolder.tv_name.setText(mList.get(position).getmName());
return convertView;
}
public static class ViewHolder {
private TextView tv_letter, tv_name;
public ViewHolder(View convertView) {
tv_letter = convertView.findViewById(R.id.tv_letter);
tv_name = convertView.findViewById(R.id.tv_name);
}
public static ViewHolder getViewHolder(View convertView) {
ViewHolder viewHolder = (ViewHolder) convertView.getTag();
if (viewHolder == null) {
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}
return viewHolder;
}
}
}
list_item.class
PinYinlinfo.java
package com.example.custom_view;
import android.text.TextUtils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class PinYinUtils {
public static String getPinYin(String str) {
String pinyin = "";
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
if (!TextUtils.isEmpty(str)) {
char[] mCharArr = str.toCharArray();
if (mCharArr != null && mCharArr.length > 0) {
for (int i = 0; i < mCharArr.length; i++) {
char c = mCharArr[i];
// TODO UTF-8格式下 1个汉字==2个字节 1个字节(-128----127)
if (c > 127) {
//不一定是汉字,也不一定是字母
try {
String[] strArr = PinyinHelper.toHanyuPinyinStringArray(c, format);
pinyin += strArr[0];
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
pinyin += c;
}
} else {
//一定不是汉字,有可能是字母或者标点或者数字
pinyin += c;
}
}
}
}
return pinyin;
}
}
PinYinUtils.java
package com.example.custom_view;
import android.text.TextUtils;
import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
public class PinYinUtils {
public static String getPinYin(String str) {
String pinyin = "";
HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
format.setCaseType(HanyuPinyinCaseType.UPPERCASE);
format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
if (!TextUtils.isEmpty(str)) {
char[] mCharArr = str.toCharArray();
if (mCharArr != null && mCharArr.length > 0) {
for (int i = 0; i < mCharArr.length; i++) {
char c = mCharArr[i];
// TODO UTF-8格式下 1个汉字==2个字节 1个字节(-128----127)
if (c > 127) {
//不一定是汉字,也不一定是字母
try {
String[] strArr = PinyinHelper.toHanyuPinyinStringArray(c, format);
pinyin += strArr[0];
} catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
badHanyuPinyinOutputFormatCombination.printStackTrace();
pinyin += c;
}
} else {
//一定不是汉字,有可能是字母或者标点或者数字
pinyin += c;
}
}
}
}
return pinyin;
}
}
终于完成了,有什么问题可以评论!