定义taginfo 类
自定义控件 SideBar
public class SidBarView extends View{
private String[] character = { "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 mPaint = new Paint();
private TextView mTextView;
/**
* 点击到的字符
*/
private int choose = -1;
/**
* 效果:按住控件时,会显现字母
* @param textView
*/
public void setTextView(TextView textView){
mTextView = textView;
}
public SidBarView(Context context) {
super(context);
}
public SidBarView(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
自定义监听
public interface OnselectCharListener{
void onSelect(String s);
}
private OnselectCharListener mOnselectChar;
public void setOnselectCharListener(OnselectCharListener onselectCharListener){
mOnselectChar = onselectCharListener;
}
点击事件
@Override
public boolean onTouchEvent(MotionEvent event) {
/**
* 获取 Y 方向的点击位置
*/
float downY = event.getY();
// 当前点的位置 / 总高度 * 字体的高度
int c = (int) (downY / getHeight() * character.length);
switch (event.getAction()){
case MotionEvent.ACTION_UP:
setBackgroundColor(Color.TRANSPARENT);
mTextView.setVisibility(GONE);
choose = -1;
invalidate();
break;
default:
setBackgroundResource(R.drawable.sidebar_bg);
/**
* 按下去
* 判断c是否在集合长度内
* 触发绘制
* 显示效果
* 文字传入 效果里显示为选中的字母
*/
if (c >= 0 && c < character.length){
choose = c;
invalidate();
mTextView.setVisibility(VISIBLE);
mTextView.setText(character[c]);
if (mOnselectChar != null){
mOnselectChar.onSelect(character[c]);
}
}
break;
}
return true; //自己处理
}
绘制
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int width = getWidth();
int singleHeight = getHeight() / character.length; //Y方向为竖直
for (int i = 0; i < character.length; i++) {
mPaint.setColor(Color.DKGRAY);
mPaint.setAntiAlias(true); //抗锯齿
mPaint.setTextSize(24);
/**
* 当前字母选中时出现的效果
*/
if (choose == i){
mPaint.setColor(Color.YELLOW);
mPaint.setTextScaleX(1.6f);
}
int xTo = (int) (getWidth() / 2 - mPaint.measureText(character[i]));
canvas.drawText(character[i], xTo ,(i + 1)*singleHeight, mPaint);
mPaint.reset();
}
}
listview 内容处理
public class ListViewActivity extends BaseActivity{
private ListView mLvTag;
private List<TagInfo> mTagInfos;
private TextView mTvChar;
private SidBarView mSidebar;
@Override
protected int findId() {
return R.layout.activity_taglist;
}
@Override
protected void initialView() {
mLvTag = (ListView) findViewById(R.id.lv_tag);
mTvChar = (TextView) findViewById(R.id.tv_char);
mSidebar = (SidBarView) findViewById(R.id.sidebar);
mSidebar.setTextView(mTvChar);
}
@Override
protected void initialListenner() {
mSidebar.setOnselectCharListener(new SidBarView.OnselectCharListener() {
@Override
public void onSelect(String s) {
int firstIndex = mTagAdapter.getFirstIndex(s); //获取 S 所在集合的脚标
mLvTag.setSelection(firstIndex); //选中当前位置
}
});
}
@Override
protected void initialData() {
mTagInfoList = getData();
mTagInfos = new ArrayList<>();
/**
* 对集合里面进行排序
*/
Collections.sort(mTagInfoList, new TitleComparator());
mTagInfos.addAll(mTagInfoList);
mTagAdapter = new TagAdapter();
mLvTag.setAdapter(mTagAdapter);
}
private List<TagInfo> mTagInfoList;
private List<TagInfo> getData(){
List<TagInfo> tagList = new ArrayList<>();
/**
* 将副标题 放入集合
*/
String[] tagArray = getResources().getStringArray(R.array.listpersons);
for (int i = 0; i < tagArray.length; i++) {
String str = tagArray[i]; //数组转化成字符串
TagInfo tagInfo = new TagInfo();
tagInfo.setTitle(str);
String pinyin = PinyinHelper.getShortPinyin(str);
tagInfo.setTitlePinyin(pinyin);
int Asic = pinyin.toUpperCase().charAt(0); //拼音 转化 哈希码值
if (Asic >= 65 && Asic <= 90){
tagInfo.setFirstPinyin(pinyin.substring(0,1).toUpperCase());
}else {
tagInfo.setFirstPinyin("#");
}
tagList.add(tagInfo);
}
return tagList;
}
@Override
public void onClick(View v) {
}
private TagAdapter mTagAdapter;
private class TagAdapter extends BaseAdapter{
@Override
public int getCount() {
if (mTagInfos!= null){
return mTagInfos.size();
}
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null){
convertView = View.inflate(ListViewActivity.this,R.layout.item_taglist,null);
viewHolder = new ViewHolder(convertView);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
// 获取数据
TagInfo tagInfo = mTagInfos.get(position);
/**
* 拿到副标题拼音的首字母
* 获得该首字母在集合中的位置
*/
int firstIndex = getFirstIndex(tagInfo.getFirstPinyin());
/**
* 依据首字母归类好
* 避免重复
*/
if (firstIndex == position){
viewHolder.tvTag.setVisibility(View.VISIBLE);
viewHolder.tvTag.setText(tagInfo.getFirstPinyin());
}else {
viewHolder.tvTag.setVisibility(View.GONE);
}
viewHolder.tvDes.setText(tagInfo.getTitle());
return convertView;
}
/**
* 返回副标题所在集合的脚标
* @param name
* @return
*/
private int getFirstIndex(String name){
for (int i = 0; i < mTagInfos.size(); i++) {
if (mTagInfos.get(i).getFirstPinyin().equals(name)){
return i;
}
}
return -1;
}
class ViewHolder{
TextView tvTag;
TextView tvDes;
public ViewHolder(View itemView){
tvTag = (TextView) itemView.findViewById(R.id.item_tv_tag);
tvDes = (TextView) itemView.findViewById(R.id.item_tv_des);
}
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
/**
* 副标题首字母比较
*/
private class TitleComparator implements Comparator<TagInfo>{
@Override
public int compare(TagInfo o1, TagInfo o2) {
if (o1.getFirstPinyin().charAt(0) < 65 || o1.getFirstPinyin().charAt(0) > 90){
return 1;
}else if (o2.getFirstPinyin().charAt(0) < 65 || o2.getFirstPinyin().charAt(0) > 90){
return -1;
}
return o1.getFirstPinyin().compareTo(o2.getFirstPinyin());
}
}
}
两个XML
1.
<ListView
android:id="@+id/lv_tag"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
<TextView
android:id="@+id/tv_char"
android:padding="15dp"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A"
android:layout_centerInParent="true"
android:background="@drawable/pinyin_select_bg"/>
<android.at.com.matchbox.tag_list.SidBarView
android:id="@+id/sidebar"
android:layout_marginBottom="20dp"
android:layout_centerVertical="true"
android:layout_marginRight="10dp"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"/>
2.
<TextView
android:id="@+id/item_tv_tag"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="A"
android:textSize="20sp"
android:padding="5dp"
android:background="#fdd9a5"/>
<TextView
android:id="@+id/item_tv_des"
android:textSize="15sp"
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="副标题"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#a4a4a4"></View>
FIN