写了个SiderBar,可以通过点击上面的字母实现对listview的快速定位,简单的写下对listview填充的数据,从A--Z排序
效果图:
SiderBar.java
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
/**
* Created by Administrator on 2016/5/4.
*/
public class SiderBar extends View {
private Paint paint;
private MyAdapter myAdapter;
private TextView textView;
private ListView listView;
private String alphabet[];//26个字母
public SiderBar(Context context) {
this(context, null);
}
public void setListViewAndAdpter(ListView listView, MyAdapter myAdapter) {
this.listView = listView;
this.myAdapter = myAdapter;
}
public SiderBar(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public void setTextViewDialog(TextView textViewDialog) {
this.textView = textViewDialog;
}
public void init() {
alphabet = 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"};
//画笔的初始化
paint = new Paint();
paint.setAntiAlias(true);
paint.setFakeBoldText(true);
paint.setTextSize(23f);
paint.setColor(Color.parseColor("#000000"));
}
/**
* 绘制26个字母
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
float letterHeight = (getHeight() - 5) / alphabet.length * 1.0f;
for (int i = 0; i < alphabet.length; i++) {
float x = getWidth() / 2 - paint.measureText(alphabet[i]) / 2;
canvas.drawText(alphabet[i], x, letterHeight * (i + 1), paint);
}
super.onDraw(canvas);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_UP:
if (textView != null)//屏幕中间呈现出一个textview
textView.setVisibility(INVISIBLE);
setBackgroundResource(android.R.color.transparent);//抬起使siderbar背景透明
invalidate();
break;
default:
setBackgroundResource(R.drawable.backgroud_drawable);//按下或移动时使siderbar背景变色
invalidate();
float touchY = event.getY();
int index = (int) (touchY / getHeight() * alphabet.length);//根据点击的y坐标获得点击字母的数组下标
if (index < alphabet.length && index >= 0) {
String currentLetter = alphabet[index];
findTouchLetter(currentLetter);
if (textView.getVisibility() == INVISIBLE)
textView.setVisibility(VISIBLE);
textView.setText(currentLetter);
}
break;
}
return true;
}
//根据点击的字母让listview定位到相应的位置
public void findTouchLetter(String letter) {
for (int i = 0; i < listView.getCount(); i++) {
if (letter == myAdapter.getItem(i)) {
listView.setSelection(i);
}
}
}
}
MyAdapter.java
import android.content.Context;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by Administrator on 2016/5/4.
*/
public class MyAdapter extends BaseAdapter {
private List list;
private Context context;
public MyAdapter(List list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_view, null);
viewHolder.textView = (TextView) convertView.findViewById(R.id.itemTextView);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
for (char i = 'A'; i <= 'Z'; i++) {
if (list.get(position).equals(i + "") || list.get(position).equals("#")) {
convertView.setBackgroundColor(Color.parseColor("#f1f1fa"));
break;
} else {
convertView.setBackgroundColor(Color.parseColor("#ffffff"));
}
}
viewHolder.textView.setText(list.get(position));
return convertView;
}
class ViewHolder {
TextView textView;
}
}
需要用到pinyin4j.jar第三方库,应下载并导入工程;
MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.TextView;
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;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private TextView textView;
private SiderBar siderBar;
private MyAdapter myAdapter;
private List list;
private ListView listView;
private String letter[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
textView = (TextView) findViewById(R.id.textView);
siderBar = (SiderBar) findViewById(R.id.SiderBar);
list = new ArrayList();
letter= 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"};
for (int i = 0; i < letter.length; i++) {
list.add(letter[i]);
}
list.add("Apple");
list.add("book");
list.add("要看");
list.add("原为白色,我需");
list.add("个item还");
list.add("码,别");
list.add("看代");
list.add("需要");
list.add("码,别");
list.add("看代");
list.add("需要");
list.add("需要");
list.add("白色,我");
list.add("还原为");
list.add("个item");
list.add("需要");
list.add("白色,我");
list.add("还原为");
list.add("个item");
list.add("重写");
list.add("说什么");
list.add("码,别");
list.add("看代");
list.add("需要");
list.add("需要");
list.add("白色,我");
list.add("还原为");
list.add("个item");
list.add("将那");
list.add("榜,则");
list.add("十名内出现,则改变那个item");
list.add("果出");
list.add("的颜色,");
list.add("如");
list = dealList(list);
myAdapter = new MyAdapter(list, this);
listView.setAdapter(myAdapter);
listView.setVerticalScrollBarEnabled(false);//listview滚动条隐藏
siderBar.setTextViewDialog(textView);//传入textview
siderBar.setListViewAndAdpter(listView, myAdapter);//传入listview和myAdapter
}
/**
* 获取汉字串拼音首字母,英文字符不变
*
* @param chinese
* @return
*/
public String cn2FirstSpell(String chinese) {
StringBuffer pybf = new StringBuffer();
char[] arr = chinese.toCharArray();
HanyuPinyinOutputFormat defaultFormat = new HanyuPinyinOutputFormat();
defaultFormat.setCaseType(HanyuPinyinCaseType.UPPERCASE);
defaultFormat.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
for (int i = 0; i < arr.length; i++) {
if (arr[i] > 128) {
try {
String[] _t = PinyinHelper.toHanyuPinyinStringArray(arr[i], defaultFormat);
if (_t != null) {
pybf.append(_t[0].charAt(0));
}
} catch (BadHanyuPinyinOutputFormatCombination e) {
return "";
}
} else {
pybf.append(arr[i]);
}
}
return pybf.toString().replaceAll("\\W", "").trim();
}
/**
* 获取第一个拼音字母
*
* @param string
* @return
*/
public String getFirstEnglishLetter(String string) {
String str = cn2FirstSpell(string);
String firstLetter=str.charAt(0)+"";
return firstLetter;
}
/**
* 对给定的list进行A-Z的排序
*
* @param list
* @return
*/
public List dealList(List list) {
List afterList = new ArrayList();
for (char i = 'A'; i <= 'Z'; i++) {
for (int j = 0; j < list.size(); j++) {
if ((getFirstEnglishLetter(list.get(j)).toUpperCase()).equals(i + "")) {
afterList.add(list.get(j));
}
}
}
return afterList;
}
}
activity_main.xml
backgroud_drawable.xml
textview_drawable.xml
源代码下载