赞
踩
Android 手机通讯录的实现源码
效果图
源码下载地址:http://download.csdn.net/detail/pcaxb/9116991
核心代码:
- package com.duguang.baseanimation.ui.listivew.sortlistview;
-
- import android.content.Context;
- import android.graphics.Canvas;
- import android.graphics.Color;
- import android.graphics.Paint;
- import android.graphics.Typeface;
- import android.graphics.drawable.ColorDrawable;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.view.View;
- import android.widget.TextView;
-
- import com.kayak.sortlistview.R;
-
- public class SideBar extends View {
- // 触摸事件
- private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
- // 26个字母
- public static String[] b = { "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 int choose = -1;// 选中
- private Paint paint = new Paint();
-
- private TextView mTextDialog;
-
- public void setTextView(TextView mTextDialog) {
- this.mTextDialog = mTextDialog;
- }
-
-
- public SideBar(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
-
- public SideBar(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
-
- public SideBar(Context context) {
- super(context);
- }
-
- /**
- * 重写这个方法
- */
- protected void onDraw(Canvas canvas) {
- super.onDraw(canvas);
- // 获取焦点改变背景颜色.
- int height = getHeight();// 获取对应高度
- int width = getWidth(); // 获取对应宽度
- int singleHeight = height / b.length;// 获取每一个字母的高度
-
- for (int i = 0; i < b.length; i++) {
- paint.setColor(Color.rgb(33, 65, 98));
- // paint.setColor(Color.WHITE);
- paint.setTypeface(Typeface.DEFAULT_BOLD);
- paint.setAntiAlias(true);
- paint.setTextSize(20);
- // 选中的状态
- if (i == choose) {
- paint.setColor(Color.parseColor("#3399ff"));
- paint.setFakeBoldText(true);
- }
- // x坐标等于中间-字符串宽度的一半.
- float xPos = width / 2 - paint.measureText(b[i]) / 2;
- float yPos = singleHeight * i + singleHeight;
- canvas.drawText(b[i], xPos, yPos, paint);
- paint.reset();// 重置画笔
- }
-
- }
-
- @Override
- public boolean dispatchTouchEvent(MotionEvent event) {
- final int action = event.getAction();
- final float y = event.getY();// 点击y坐标
- final int oldChoose = choose;
- final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
- final int c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
-
- switch (action) {
- case MotionEvent.ACTION_UP:
- setBackgroundDrawable(new ColorDrawable(0x00000000));
- choose = -1;//
- invalidate();
- if (mTextDialog != null) {
- mTextDialog.setVisibility(View.INVISIBLE);
- }
- break;
-
- default:
- //设置右侧字母列表[A,B,C,D,E....]的背景颜色
- setBackgroundResource(R.drawable.sortlistview_sidebar_background);
- if (oldChoose != c) {
- if (c >= 0 && c < b.length) {
- if (listener != null) {
- listener.onTouchingLetterChanged(b[c]);
- }
- if (mTextDialog != null) {
- mTextDialog.setText(b[c]);
- mTextDialog.setVisibility(View.VISIBLE);
- }
-
- choose = c;
- invalidate();
- }
- }
-
- break;
- }
- return true;
- }
-
- /**
- * 向外公开的方法
- *
- * @param onTouchingLetterChangedListener
- */
- public void setOnTouchingLetterChangedListener(
- OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
- this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
- }
-
- /**
- * 接口
- *
- * @author coder
- *
- */
- public interface OnTouchingLetterChangedListener {
- public void onTouchingLetterChanged(String s);
- }
-
- }
- package com.duguang.baseanimation.ui.listivew.sortlistview;
-
- import java.util.List;
-
- import android.content.Context;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.BaseAdapter;
- import android.widget.SectionIndexer;
- import android.widget.TextView;
-
- import com.kayak.sortlistview.R;
-
- public class SortAdapter extends BaseAdapter implements SectionIndexer{
- private List<SortModel> list = null;
- private Context mContext;
-
- public SortAdapter(Context mContext, List<SortModel> list) {
- this.mContext = mContext;
- this.list = list;
- }
-
-
- /**
- * 当ListView数据发生变化时,调用此方法来更新ListView
- * @param list
- */
- public void updateListView(List<SortModel> list){
- this.list = list;
- notifyDataSetChanged();
- }
-
- 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 = null;
- final SortModel mContent = list.get(position);
- if (view == null) {
- viewHolder = new ViewHolder();
- view = LayoutInflater.from(mContext).inflate(R.layout.item_sort_listview, null);
- viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
- viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
- view.setTag(viewHolder);
- } else {
- viewHolder = (ViewHolder) view.getTag();
- }
-
- //根据position获取分类的首字母的Char ascii值
- int section = getSectionForPosition(position);
-
- //如果当前位置等于该分类首字母的Char的位置 ,则认为是第一次出现
- if(position == getPositionForSection(section)){
- viewHolder.tvLetter.setVisibility(View.VISIBLE);
- viewHolder.tvLetter.setText(mContent.getSortLetters());
- }else{
- viewHolder.tvLetter.setVisibility(View.GONE);
- }
-
- viewHolder.tvTitle.setText(this.list.get(position).getName());
-
- return view;
-
- }
-
-
-
- final static class ViewHolder {
- TextView tvLetter;
- TextView tvTitle;
- }
-
-
- /**
- * 根据ListView的当前位置获取分类的首字母的Char ascii值
- */
- public int getSectionForPosition(int position) {
- return list.get(position).getSortLetters().charAt(0);
- }
-
- /**
- * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
- */
- public int getPositionForSection(int section) {
- for (int i = 0; i < getCount(); i++) {
- String sortStr = list.get(i).getSortLetters();
- char firstChar = sortStr.toUpperCase().charAt(0);
- if (firstChar == section) {
- return i;
- }
- }
-
- return -1;
- }
-
- /**
- * 提取英文的首字母,非英文字母用#代替。
- *
- * @param str
- * @return
- */
- private String getAlpha(String str) {
- String sortStr = str.trim().substring(0, 1).toUpperCase();
- // 正则表达式,判断首字母是否是英文字母
- if (sortStr.matches("[A-Z]")) {
- return sortStr;
- } else {
- return "#";
- }
- }
-
- @Override
- public Object[] getSections() {
- return null;
- }
- }
- package com.duguang.baseanimation.ui.listivew.sortlistview;
-
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.List;
-
- import android.text.Editable;
- import android.text.TextUtils;
- import android.text.TextWatcher;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.AdapterView.OnItemClickListener;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
-
- import com.duguang.baseanimation.ui.base.BaseActivity;
- import com.duguang.baseanimation.ui.listivew.sortlistview.SideBar.OnTouchingLetterChangedListener;
- import com.kayak.sortlistview.R;
-
- /**
- * 通讯录效果一
- * @author Administrator
- *
- */
- public class SortListViewMainActivity extends BaseActivity {
- private ListView sortListView;
- private SideBar sideBar;
- private TextView dialog;
- private SortAdapter adapter;
- private ClearEditText mClearEditText;
-
-
- /**
- * 汉字转换成拼音的类
- */
- private CharacterParser characterParser;
- private List<SortModel> SourceDateList;
-
- /**
- * 根据拼音来排列ListView里面的数据类
- */
- private PinyinComparator pinyinComparator;
-
-
- @Override
- public void setView() {
- setContentView(R.layout.activity_listview_sort_main);
-
- }
-
- @Override
- public void initView() {
- //实例化汉字转拼音类
- characterParser = CharacterParser.getInstance();
-
- pinyinComparator = new PinyinComparator();
-
- sideBar = (SideBar) findViewById(R.id.sidrbar);
- dialog = (TextView) findViewById(R.id.dialog);
- sideBar.setTextView(dialog);
-
- //设置右侧触摸监听
- sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
-
- @Override
- public void onTouchingLetterChanged(String s) {
- //该字母首次出现的位置
- int position = adapter.getPositionForSection(s.charAt(0));
- if(position != -1){
- sortListView.setSelection(position);
- }
-
- }
- });
-
- sortListView = (ListView) findViewById(R.id.country_lvcountry);
- sortListView.setOnItemClickListener(new OnItemClickListener() {
-
- @Override
- public void onItemClick(AdapterView<?> parent, View view,
- int position, long id) {
- //这里要利用adapter.getItem(position)来获取当前position所对应的对象
- Toast.makeText(getApplication(), ((SortModel)adapter.getItem(position)).getName(), Toast.LENGTH_SHORT).show();
- }
- });
-
- SourceDateList = filledData(getResources().getStringArray(R.array.date));
-
- // 根据a-z进行排序源数据
- Collections.sort(SourceDateList, pinyinComparator);
- adapter = new SortAdapter(this, SourceDateList);
- sortListView.setAdapter(adapter);
-
-
- mClearEditText = (ClearEditText) findViewById(R.id.filter_edit);
-
- //根据输入框输入值的改变来过滤搜索
- mClearEditText.addTextChangedListener(new TextWatcher() {
-
- @Override
- public void onTextChanged(CharSequence s, int start, int before, int count) {
- //当输入框里面的值为空,更新为原来的列表,否则为过滤数据列表
- filterData(s.toString());
- }
-
- @Override
- public void beforeTextChanged(CharSequence s, int start, int count,
- int after) {
-
- }
-
- @Override
- public void afterTextChanged(Editable s) {
- }
- });
-
- }
-
- @Override
- public void setListener() {
- // TODO Auto-generated method stub
-
- }
-
-
- /**
- * 为ListView填充数据
- * @param date
- * @return
- */
- private List<SortModel> filledData(String [] date){
- List<SortModel> mSortList = new ArrayList<SortModel>();
-
- for(int i=0; i<date.length; i++){
- SortModel sortModel = new SortModel();
- sortModel.setName(date[i]);
- //汉字转换成拼音
- String pinyin = characterParser.getSelling(date[i]);
- String sortString = pinyin.substring(0, 1).toUpperCase();
-
- // 正则表达式,判断首字母是否是英文字母
- if(sortString.matches("[A-Z]")){
- sortModel.setSortLetters(sortString.toUpperCase());
- }else{
- sortModel.setSortLetters("#");
- }
-
- mSortList.add(sortModel);
- }
- return mSortList;
-
- }
-
- /**
- * 根据输入框中的值来过滤数据并更新ListView
- * @param filterStr
- */
- private void filterData(String filterStr){
- List<SortModel> filterDateList = new ArrayList<SortModel>();
-
- if(TextUtils.isEmpty(filterStr)){
- filterDateList = SourceDateList;
- }else{
- filterDateList.clear();
- for(SortModel sortModel : SourceDateList){
- String name = sortModel.getName();
- if(name.indexOf(filterStr.toString()) != -1 || characterParser.getSelling(name).startsWith(filterStr.toString())){
- filterDateList.add(sortModel);
- }
- }
- }
-
- // 根据a-z进行排序
- Collections.sort(filterDateList, pinyinComparator);
- adapter.updateListView(filterDateList);
- }
-
-
- }
源码下载地址:http://download.csdn.net/detail/pcaxb/9116991
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。