先上效果圖


1.需要字Build中參考
//將文字轉化為漢語拼音 implementation 'com.belerweb:pinyin4j:2.5.1' implementation 'com.android.support:recyclerview-v7:28.0.0'
2.創建 Cn2Spell 類
/**
* 漢字轉換位漢語拼音,英文字符不變
*/
public class Cn2Spell {
public static StringBuffer sb = new StringBuffer();
/**
* 獲取漢字字串的首字母,英文字符不變
* 例如:阿飛→af
*/
public static String getPinYinHeadChar(String chines) {
sb.setLength(0);
char[] chars = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < chars.length; i++) {
if (chars[i] > 128) {
try {
sb.append(PinyinHelper.toHanyuPinyinStringArray(chars[i], defaultFormat)[0].charAt(0));
} catch (Exception e) {
e.printStackTrace();
}
} else {
sb.append(chars[i]);
}
}
return sb.toString();
}
/**
* 獲取漢字字串的第一個字母
*/
public static String getPinYinFirstLetter(String str) {
sb.setLength(0);
char c = str.charAt(0);
String[] pinyinArray = PinyinHelper.toHanyuPinyinStringArray(c);
if (pinyinArray != null) {
sb.append(pinyinArray[0].charAt(0));
} else {
sb.append(c);
}
return sb.toString();
}
/**
* 獲取漢字字串的漢語拼音,英文字符不變
*/
public static String getPinYin(String chines) {
sb.setLength(0);
char[] nameChar = chines.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.LOWERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < nameChar.length; i++) {
if (nameChar[i] > 128) {
try {
sb.append(PinyinHelper.toHanyuPinyinStringArray(nameChar[i], defaultFormat)[0]);
} catch (Exception e) {
e.printStackTrace();
}
} else {
sb.append(nameChar[i]);
}
}
return sb.toString();
}
}
3.創建右側 SideBar 類
public class SideBar extends TextView {
private String[] letters = 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 Paint textPaint;
private Paint bigTextPaint;
private Paint scaleTextPaint;
private Canvas canvas;
private int itemH;
private int w;
private int h;
/**
* 普通情況下字體大小
*/
float singleTextH;
/**
* 縮放離原始的寬度
*/
private float scaleWidth;
/**
* 滑動的Y
*/
private float eventY = 0;
/**
* 縮放的倍數
*/
private int scaleSize = 1;
/**
* 縮放個數item,即開口大小
*/
private int scaleItemCount = 6;
private ISideBarSelectCallBack callBack;
public SideBar(Context context) {
this(context, null);
}
public SideBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SideBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(attrs);
}
private void init(AttributeSet attrs) {
if (attrs != null) {
TypedArray ta = getContext().obtainStyledAttributes(attrs, R.styleable.SideBar);
scaleSize = ta.getInteger(R.styleable.SideBar_scaleSize, 1);
scaleItemCount = ta.getInteger(R.styleable.SideBar_scaleItemCount, 6);
scaleWidth = ta.getDimensionPixelSize(R.styleable.SideBar_scaleWidth, dp(100));
ta.recycle();
}
textPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
textPaint.setColor(getCurrentTextColor());
textPaint.setTextSize(getTextSize());
textPaint.setTextAlign(Paint.Align.CENTER);
bigTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
bigTextPaint.setColor(getCurrentTextColor());
bigTextPaint.setTextSize(getTextSize() * (scaleSize + 3));
bigTextPaint.setTextAlign(Paint.Align.CENTER);
scaleTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
scaleTextPaint.setColor(getCurrentTextColor());
scaleTextPaint.setTextSize(getTextSize() * (scaleSize + 1));
scaleTextPaint.setTextAlign(Paint.Align.CENTER);
}
public void setDataResource(String[] data) {
letters = data;
invalidate();
}
public void setOnStrSelectCallBack(ISideBarSelectCallBack callBack) {
this.callBack = callBack;
}
/**
* 設定字體縮放比例
*
* @param scale
*/
public void setScaleSize(int scale) {
scaleSize = scale;
invalidate();
}
/**
* 設定縮放字體的個數,即開口大小
*
* @param scaleItemCount
*/
public void setScaleItemCount(int scaleItemCount) {
this.scaleItemCount = scaleItemCount;
invalidate();
}
private int dp(int px) {
final float scale = getContext().getResources().getDisplayMetrics().density;
return (int) (px * scale + 0.5f);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
if (event.getX() > (w - getPaddingRight() - singleTextH - 10)) {
eventY = event.getY();
invalidate();
return true;
} else {
eventY = 0;
invalidate();
break;
}
case MotionEvent.ACTION_CANCEL:
eventY = 0;
invalidate();
return true;
case MotionEvent.ACTION_UP:
if (event.getX() > (w - getPaddingRight() - singleTextH - 10)) {
eventY = 0;
invalidate();
return true;
} else
break;
}
return super.onTouchEvent(event);
}
@Override
protected void onDraw(Canvas canvas) {
this.canvas = canvas;
DrawView(eventY);
}
private void DrawView(float y) {
int currentSelectIndex = -1;
if (y != 0) {
for (int i = 0; i < letters.length; i++) {
float currentItemY = itemH * i;
float nextItemY = itemH * (i + 1);
if (y >= currentItemY && y < nextItemY) {
currentSelectIndex = i;
if (callBack != null) {
callBack.onSelectStr(currentSelectIndex, letters[i]);
}
//畫大的字母
Paint.FontMetrics fontMetrics = bigTextPaint.getFontMetrics();
float bigTextSize = fontMetrics.descent - fontMetrics.ascent;
canvas.drawText(letters[i], w - getPaddingRight() - scaleWidth - bigTextSize, singleTextH + itemH * i, bigTextPaint);
}
}
}
drawLetters(y, currentSelectIndex);
}
private void drawLetters(float y, int index) {
//第一次進來沒有縮放情況,默認畫原圖
if (index == -1) {
w = getMeasuredWidth();
h = getMeasuredHeight();
itemH = h / letters.length;
Paint.FontMetrics fontMetrics = textPaint.getFontMetrics();
singleTextH = fontMetrics.descent - fontMetrics.ascent;
for (int i = 0; i < letters.length; i++) {
canvas.drawText(letters[i], w - getPaddingRight(), singleTextH + itemH * i, textPaint);
}
//觸摸的時候畫縮放圖
} else {
//遍歷所有字母
for (int i = 0; i < letters.length; i++) {
//要畫的字母的起始Y坐標
float currentItemToDrawY = singleTextH + itemH * i;
float centerItemToDrawY;
if (index < i)
centerItemToDrawY = singleTextH + itemH * (index + scaleItemCount);
else
centerItemToDrawY = singleTextH + itemH * (index - scaleItemCount);
float delta = 1 - Math.abs((y - currentItemToDrawY) / (centerItemToDrawY - currentItemToDrawY));
float maxRightX = w - getPaddingRight();
//如果大于0,表明在y坐標上方
scaleTextPaint.setTextSize(getTextSize() + getTextSize() * delta);
float drawX = maxRightX - scaleWidth * delta;
//超出邊界直接花在邊界上
if (drawX > maxRightX)
canvas.drawText(letters[i], maxRightX, singleTextH + itemH * i, textPaint);
else
canvas.drawText(letters[i], drawX, singleTextH + itemH * i, scaleTextPaint);
}
}
}
public interface ISideBarSelectCallBack {
void onSelectStr(int index, String selectStr);
}
}
或者LetterSideBar
public class LetterSideBar extends View {
private Paint mPaint;
private boolean isUp = false;
// 定義26個字母
public static String[] mLetters = {"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 String mCurrentTouchLetter;
private int currentPosition;
public LetterSideBar(Context context) {
this(context, null);
}
public LetterSideBar(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LetterSideBar(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
mPaint.setAntiAlias(true);
// 自定義屬性,顏色 字體大小
mPaint.setTextSize(sp2px(12));// 設定的是像素
// 默認顏色
mPaint.setColor(Color.BLUE);
}
// sp 轉 px
private float sp2px(int sp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
sp, getResources().getDisplayMetrics());
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
// 計算指定寬度 = 左右的padding + 字母的寬度(取決于你的畫筆)
int textWidth = (int) mPaint.measureText("A");// A字母的寬度
int width = getPaddingLeft() + getPaddingRight() + textWidth;
// 高度可以直接獲取
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width, height);
}
@Override
protected void onDraw(Canvas canvas) {
// 畫26個字母
int itemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLetters.length;
for (int i = 0; i < mLetters.length; i++) {
// 知道每個字母的中心位置 1 字母的高度一半 2 字母高度一般+前面字符的高度
int letterCenterY = i * itemHeight + itemHeight / 2 + getPaddingTop();
// 基線,基于中心位置, 知道中心位置還不會基線,看一下之前的視頻
Paint.FontMetrics fontMetrics = mPaint.getFontMetrics();
int dy = (int) ((fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom);
int baseLine = letterCenterY + dy;
// x 繪制在最中間 = 寬度/2 - 文字/2
int textWidth = (int) mPaint.measureText(mLetters[i]);
int x = getWidth() / 2 - textWidth / 2;
if (!isUp){
// 當前字母 高亮 用兩個畫筆(最好) 改變顏色
if (mLetters[i].equals(mCurrentTouchLetter)) {
mPaint.setColor(Color.RED);
canvas.drawText(mLetters[i], x, baseLine, mPaint);
} else {
mPaint.setColor(Color.BLUE);
canvas.drawText(mLetters[i], x, baseLine, mPaint);
}
}else {
mPaint.setColor(Color.BLUE);
canvas.drawText(mLetters[i], x, baseLine, mPaint);
}
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
// 計算出當前觸摸字母 獲取當前的位置
float currentMoveY = event.getY();
// 位置 = currentMoveY / 字母高度 , 通過位置獲取字母 優化?
int itemHeight = (getHeight() - getPaddingTop() - getPaddingBottom()) / mLetters.length;
currentPosition = (int) (currentMoveY / itemHeight);
if (currentPosition < 0)
currentPosition = 0;
if (currentPosition > mLetters.length - 1)
currentPosition = mLetters.length - 1;
// 要判斷 ?
mCurrentTouchLetter = mLetters[currentPosition];
if (mListener != null) {
mListener.touch(mCurrentTouchLetter, true, currentPosition);
}
isUp = false;
// 重新繪制
invalidate();
break;
case MotionEvent.ACTION_UP:
if (mListener != null) {
mListener.touch(mCurrentTouchLetter, false, currentPosition);
}
isUp = true;
// 重新繪制
invalidate();
break;
}
return true;
}
private LetterTouchListener mListener;
public void setOnLetterTouchListener(LetterTouchListener listener) {
this.mListener = listener;
}
// 介面回掉其他View會不會使用?
public interface LetterTouchListener {
void touch(CharSequence letter, boolean isTouch, int currentPosition);
}
}
4.創建物體類,進行排序等操作
public class User implements Comparable<User> {
private String name; // 姓名
private String pinyin; // 姓名對應的拼音
private String firstLetter; // 拼音的首字母
public User() {
}
public User(String name) {
this.name = name;
pinyin = Cn2Spell.getPinYin(name); // 根據姓名獲取拼音
firstLetter = pinyin.substring(0, 1).toUpperCase(); // 獲取拼音首字母并轉成大寫
if (!firstLetter.matches("[A-Z]")) { // 如果不在A-Z中則默認為“#”
firstLetter = "#";
}
}
public String getName() {
return name;
}
public String getPinyin() {
return pinyin;
}
public String getFirstLetter() {
return firstLetter;
}
@Override
public int compareTo(User another) {
if (firstLetter.equals("#") && !another.getFirstLetter().equals("#")) {
return 1;
} else if (!firstLetter.equals("#") && another.getFirstLetter().equals("#")){
return -1;
} else {
return pinyin.compareToIgnoreCase(another.getPinyin());
}
}
}
5.配接器
public class SortAdapter extends BaseAdapter{
private List<User> list = null;
private Context mContext;
public SortAdapter(Context mContext, List<User> list) {
this.mContext = mContext;
this.list = list;
}
public int getCount() {
return this.list.size();
}
public Object getItem(int position) {
return list.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View view, ViewGroup arg2) {
ViewHolder viewHolder;
final User user = list.get(position);
if (view == null) {
viewHolder = new ViewHolder();
view = LayoutInflater.from(mContext).inflate(R.layout.item, null);
viewHolder.name = (TextView) view.findViewById(R.id.name);
viewHolder.catalog = (TextView) view.findViewById(R.id.catalog);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
//根據position獲取首字母作為目錄catalog
String catalog = list.get(position).getFirstLetter();
//如果當前位置等于該分類首字母的Char的位置 ,則認為是第一次出現
if(position == getPositionForSection(catalog)){
viewHolder.catalog.setVisibility(View.VISIBLE);
viewHolder.catalog.setText(user.getFirstLetter().toUpperCase());
}else{
viewHolder.catalog.setVisibility(View.GONE);
}
viewHolder.name.setText(this.list.get(position).getName());
return view;
}
final static class ViewHolder {
TextView catalog;
TextView name;
}
/**
* 獲取catalog首次出現位置
*/
public int getPositionForSection(String catalog) {
for (int i = 0; i < getCount(); i++) {
String sortStr = list.get(i).getFirstLetter();
if (catalog.equalsIgnoreCase(sortStr)) {
return i;
}
}
return -1;
}
}
6.配接器的XML
<?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="wrap_content"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="@+id/catalog"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E0E0E0"
android:textColor="#454545"
android:textSize="20sp"
android:padding="10dp"/>
<TextView
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:textColor="#336598"
android:textSize="16sp"
android:padding="10dp"/>
</LinearLayout>
7.主Activity
public class MainActivity extends AppCompatActivity {
private ListView listView;
private SideBar sideBar;
private ArrayList<User> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
}
private void initView() {
listView = (ListView) findViewById(R.id.listView);
sideBar = (SideBar) findViewById(R.id.side_bar);
sideBar.setOnStrSelectCallBack(new SideBar.ISideBarSelectCallBack() {
@Override
public void onSelectStr(int index, String selectStr) {
for (int i = 0; i < list.size(); i++) {
if (selectStr.equalsIgnoreCase(list.get(i).getFirstLetter())) {
listView.setSelection(i); // 選擇到首字母出現的位置
return;
}
}
}
});
}
private void initData() {
list = new ArrayList<>();
list.add(new User("張琪"));
list.add(new User("柯南"));
list.add(new User("羅峰"));
list.add(new User("林動"));
list.add(new User("武俠"));
list.add(new User("鳴人"));
list.add(new User("佐助"));
list.add(new User("我愛羅"));
list.add(new User("卡卡西"));
list.add(new User("如來"));
list.add(new User("沸羊羊"));
list.add(new User("暖羊羊"));
list.add(new User("慢羊羊"));
list.add(new User("灰太狼"));
list.add(new User("紅太狼"));
list.add(new User("孫悟空"));
list.add(new User("玉帝"));
list.add(new User("舒克"));
list.add(new User("貝塔"));
list.add(new User("海爾"));
list.add(new User("阿凡提"));
list.add(new User("邋遢大王"));
list.add(new User("哪吒"));
list.add(new User("沒頭腦"));
list.add(new User("不高興"));
list.add(new User("藍皮鼠"));
list.add(new User("大臉貓"));
list.add(new User("大頭兒子"));
list.add(new User("小頭爸爸"));
list.add(new User("藍貓"));
list.add(new User("淘氣"));
list.add(new User("葉峰"));
list.add(new User("楚天歌"));
list.add(new User("江流兒"));
list.add(new User("Tom"));
list.add(new User("Jerry"));
list.add(new User("12345"));
list.add(new User("54321"));
list.add(new User("_(:з」∠)_"));
list.add(new User("……%¥#¥%#"));
Collections.sort(list); // 對list進行排序,需要讓User實作Comparable介面重寫compareTo方法
SortAdapter adapter = new SortAdapter(this, list);
listView.setAdapter(adapter);
}
}
Demo下載地址:https://download.csdn.net/download/yyxhzdm/15383959
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/262074.html
標籤:其他
