当前位置:   article > 正文

Android 手机通讯录的实现源码_android通讯录开发完整代码

android通讯录开发完整代码

Android 手机通讯录的实现源码

效果图


源码下载地址:http://download.csdn.net/detail/pcaxb/9116991


核心代码:

  1. package com.duguang.baseanimation.ui.listivew.sortlistview;
  2. import android.content.Context;
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.Typeface;
  7. import android.graphics.drawable.ColorDrawable;
  8. import android.util.AttributeSet;
  9. import android.view.MotionEvent;
  10. import android.view.View;
  11. import android.widget.TextView;
  12. import com.kayak.sortlistview.R;
  13. public class SideBar extends View {
  14. // 触摸事件
  15. private OnTouchingLetterChangedListener onTouchingLetterChangedListener;
  16. // 26个字母
  17. public static String[] b = { "A", "B", "C", "D", "E", "F", "G", "H", "I",
  18. "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V",
  19. "W", "X", "Y", "Z", "#" };
  20. private int choose = -1;// 选中
  21. private Paint paint = new Paint();
  22. private TextView mTextDialog;
  23. public void setTextView(TextView mTextDialog) {
  24. this.mTextDialog = mTextDialog;
  25. }
  26. public SideBar(Context context, AttributeSet attrs, int defStyle) {
  27. super(context, attrs, defStyle);
  28. }
  29. public SideBar(Context context, AttributeSet attrs) {
  30. super(context, attrs);
  31. }
  32. public SideBar(Context context) {
  33. super(context);
  34. }
  35. /**
  36. * 重写这个方法
  37. */
  38. protected void onDraw(Canvas canvas) {
  39. super.onDraw(canvas);
  40. // 获取焦点改变背景颜色.
  41. int height = getHeight();// 获取对应高度
  42. int width = getWidth(); // 获取对应宽度
  43. int singleHeight = height / b.length;// 获取每一个字母的高度
  44. for (int i = 0; i < b.length; i++) {
  45. paint.setColor(Color.rgb(33, 65, 98));
  46. // paint.setColor(Color.WHITE);
  47. paint.setTypeface(Typeface.DEFAULT_BOLD);
  48. paint.setAntiAlias(true);
  49. paint.setTextSize(20);
  50. // 选中的状态
  51. if (i == choose) {
  52. paint.setColor(Color.parseColor("#3399ff"));
  53. paint.setFakeBoldText(true);
  54. }
  55. // x坐标等于中间-字符串宽度的一半.
  56. float xPos = width / 2 - paint.measureText(b[i]) / 2;
  57. float yPos = singleHeight * i + singleHeight;
  58. canvas.drawText(b[i], xPos, yPos, paint);
  59. paint.reset();// 重置画笔
  60. }
  61. }
  62. @Override
  63. public boolean dispatchTouchEvent(MotionEvent event) {
  64. final int action = event.getAction();
  65. final float y = event.getY();// 点击y坐标
  66. final int oldChoose = choose;
  67. final OnTouchingLetterChangedListener listener = onTouchingLetterChangedListener;
  68. final int c = (int) (y / getHeight() * b.length);// 点击y坐标所占总高度的比例*b数组的长度就等于点击b中的个数.
  69. switch (action) {
  70. case MotionEvent.ACTION_UP:
  71. setBackgroundDrawable(new ColorDrawable(0x00000000));
  72. choose = -1;//
  73. invalidate();
  74. if (mTextDialog != null) {
  75. mTextDialog.setVisibility(View.INVISIBLE);
  76. }
  77. break;
  78. default:
  79. //设置右侧字母列表[A,B,C,D,E....]的背景颜色
  80. setBackgroundResource(R.drawable.sortlistview_sidebar_background);
  81. if (oldChoose != c) {
  82. if (c >= 0 && c < b.length) {
  83. if (listener != null) {
  84. listener.onTouchingLetterChanged(b[c]);
  85. }
  86. if (mTextDialog != null) {
  87. mTextDialog.setText(b[c]);
  88. mTextDialog.setVisibility(View.VISIBLE);
  89. }
  90. choose = c;
  91. invalidate();
  92. }
  93. }
  94. break;
  95. }
  96. return true;
  97. }
  98. /**
  99. * 向外公开的方法
  100. *
  101. * @param onTouchingLetterChangedListener
  102. */
  103. public void setOnTouchingLetterChangedListener(
  104. OnTouchingLetterChangedListener onTouchingLetterChangedListener) {
  105. this.onTouchingLetterChangedListener = onTouchingLetterChangedListener;
  106. }
  107. /**
  108. * 接口
  109. *
  110. * @author coder
  111. *
  112. */
  113. public interface OnTouchingLetterChangedListener {
  114. public void onTouchingLetterChanged(String s);
  115. }
  116. }

  1. package com.duguang.baseanimation.ui.listivew.sortlistview;
  2. import java.util.List;
  3. import android.content.Context;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. import android.widget.BaseAdapter;
  8. import android.widget.SectionIndexer;
  9. import android.widget.TextView;
  10. import com.kayak.sortlistview.R;
  11. public class SortAdapter extends BaseAdapter implements SectionIndexer{
  12. private List<SortModel> list = null;
  13. private Context mContext;
  14. public SortAdapter(Context mContext, List<SortModel> list) {
  15. this.mContext = mContext;
  16. this.list = list;
  17. }
  18. /**
  19. * 当ListView数据发生变化时,调用此方法来更新ListView
  20. * @param list
  21. */
  22. public void updateListView(List<SortModel> list){
  23. this.list = list;
  24. notifyDataSetChanged();
  25. }
  26. public int getCount() {
  27. return this.list.size();
  28. }
  29. public Object getItem(int position) {
  30. return list.get(position);
  31. }
  32. public long getItemId(int position) {
  33. return position;
  34. }
  35. public View getView(final int position, View view, ViewGroup arg2) {
  36. ViewHolder viewHolder = null;
  37. final SortModel mContent = list.get(position);
  38. if (view == null) {
  39. viewHolder = new ViewHolder();
  40. view = LayoutInflater.from(mContext).inflate(R.layout.item_sort_listview, null);
  41. viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);
  42. viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);
  43. view.setTag(viewHolder);
  44. } else {
  45. viewHolder = (ViewHolder) view.getTag();
  46. }
  47. //根据position获取分类的首字母的Char ascii值
  48. int section = getSectionForPosition(position);
  49. //如果当前位置等于该分类首字母的Char的位置 ,则认为是第一次出现
  50. if(position == getPositionForSection(section)){
  51. viewHolder.tvLetter.setVisibility(View.VISIBLE);
  52. viewHolder.tvLetter.setText(mContent.getSortLetters());
  53. }else{
  54. viewHolder.tvLetter.setVisibility(View.GONE);
  55. }
  56. viewHolder.tvTitle.setText(this.list.get(position).getName());
  57. return view;
  58. }
  59. final static class ViewHolder {
  60. TextView tvLetter;
  61. TextView tvTitle;
  62. }
  63. /**
  64. * 根据ListView的当前位置获取分类的首字母的Char ascii值
  65. */
  66. public int getSectionForPosition(int position) {
  67. return list.get(position).getSortLetters().charAt(0);
  68. }
  69. /**
  70. * 根据分类的首字母的Char ascii值获取其第一次出现该首字母的位置
  71. */
  72. public int getPositionForSection(int section) {
  73. for (int i = 0; i < getCount(); i++) {
  74. String sortStr = list.get(i).getSortLetters();
  75. char firstChar = sortStr.toUpperCase().charAt(0);
  76. if (firstChar == section) {
  77. return i;
  78. }
  79. }
  80. return -1;
  81. }
  82. /**
  83. * 提取英文的首字母,非英文字母用#代替。
  84. *
  85. * @param str
  86. * @return
  87. */
  88. private String getAlpha(String str) {
  89. String sortStr = str.trim().substring(0, 1).toUpperCase();
  90. // 正则表达式,判断首字母是否是英文字母
  91. if (sortStr.matches("[A-Z]")) {
  92. return sortStr;
  93. } else {
  94. return "#";
  95. }
  96. }
  97. @Override
  98. public Object[] getSections() {
  99. return null;
  100. }
  101. }

  1. package com.duguang.baseanimation.ui.listivew.sortlistview;
  2. import java.util.ArrayList;
  3. import java.util.Collections;
  4. import java.util.List;
  5. import android.text.Editable;
  6. import android.text.TextUtils;
  7. import android.text.TextWatcher;
  8. import android.view.View;
  9. import android.widget.AdapterView;
  10. import android.widget.AdapterView.OnItemClickListener;
  11. import android.widget.ListView;
  12. import android.widget.TextView;
  13. import android.widget.Toast;
  14. import com.duguang.baseanimation.ui.base.BaseActivity;
  15. import com.duguang.baseanimation.ui.listivew.sortlistview.SideBar.OnTouchingLetterChangedListener;
  16. import com.kayak.sortlistview.R;
  17. /**
  18. * 通讯录效果一
  19. * @author Administrator
  20. *
  21. */
  22. public class SortListViewMainActivity extends BaseActivity {
  23. private ListView sortListView;
  24. private SideBar sideBar;
  25. private TextView dialog;
  26. private SortAdapter adapter;
  27. private ClearEditText mClearEditText;
  28. /**
  29. * 汉字转换成拼音的类
  30. */
  31. private CharacterParser characterParser;
  32. private List<SortModel> SourceDateList;
  33. /**
  34. * 根据拼音来排列ListView里面的数据类
  35. */
  36. private PinyinComparator pinyinComparator;
  37. @Override
  38. public void setView() {
  39. setContentView(R.layout.activity_listview_sort_main);
  40. }
  41. @Override
  42. public void initView() {
  43. //实例化汉字转拼音类
  44. characterParser = CharacterParser.getInstance();
  45. pinyinComparator = new PinyinComparator();
  46. sideBar = (SideBar) findViewById(R.id.sidrbar);
  47. dialog = (TextView) findViewById(R.id.dialog);
  48. sideBar.setTextView(dialog);
  49. //设置右侧触摸监听
  50. sideBar.setOnTouchingLetterChangedListener(new OnTouchingLetterChangedListener() {
  51. @Override
  52. public void onTouchingLetterChanged(String s) {
  53. //该字母首次出现的位置
  54. int position = adapter.getPositionForSection(s.charAt(0));
  55. if(position != -1){
  56. sortListView.setSelection(position);
  57. }
  58. }
  59. });
  60. sortListView = (ListView) findViewById(R.id.country_lvcountry);
  61. sortListView.setOnItemClickListener(new OnItemClickListener() {
  62. @Override
  63. public void onItemClick(AdapterView<?> parent, View view,
  64. int position, long id) {
  65. //这里要利用adapter.getItem(position)来获取当前position所对应的对象
  66. Toast.makeText(getApplication(), ((SortModel)adapter.getItem(position)).getName(), Toast.LENGTH_SHORT).show();
  67. }
  68. });
  69. SourceDateList = filledData(getResources().getStringArray(R.array.date));
  70. // 根据a-z进行排序源数据
  71. Collections.sort(SourceDateList, pinyinComparator);
  72. adapter = new SortAdapter(this, SourceDateList);
  73. sortListView.setAdapter(adapter);
  74. mClearEditText = (ClearEditText) findViewById(R.id.filter_edit);
  75. //根据输入框输入值的改变来过滤搜索
  76. mClearEditText.addTextChangedListener(new TextWatcher() {
  77. @Override
  78. public void onTextChanged(CharSequence s, int start, int before, int count) {
  79. //当输入框里面的值为空,更新为原来的列表,否则为过滤数据列表
  80. filterData(s.toString());
  81. }
  82. @Override
  83. public void beforeTextChanged(CharSequence s, int start, int count,
  84. int after) {
  85. }
  86. @Override
  87. public void afterTextChanged(Editable s) {
  88. }
  89. });
  90. }
  91. @Override
  92. public void setListener() {
  93. // TODO Auto-generated method stub
  94. }
  95. /**
  96. * 为ListView填充数据
  97. * @param date
  98. * @return
  99. */
  100. private List<SortModel> filledData(String [] date){
  101. List<SortModel> mSortList = new ArrayList<SortModel>();
  102. for(int i=0; i<date.length; i++){
  103. SortModel sortModel = new SortModel();
  104. sortModel.setName(date[i]);
  105. //汉字转换成拼音
  106. String pinyin = characterParser.getSelling(date[i]);
  107. String sortString = pinyin.substring(0, 1).toUpperCase();
  108. // 正则表达式,判断首字母是否是英文字母
  109. if(sortString.matches("[A-Z]")){
  110. sortModel.setSortLetters(sortString.toUpperCase());
  111. }else{
  112. sortModel.setSortLetters("#");
  113. }
  114. mSortList.add(sortModel);
  115. }
  116. return mSortList;
  117. }
  118. /**
  119. * 根据输入框中的值来过滤数据并更新ListView
  120. * @param filterStr
  121. */
  122. private void filterData(String filterStr){
  123. List<SortModel> filterDateList = new ArrayList<SortModel>();
  124. if(TextUtils.isEmpty(filterStr)){
  125. filterDateList = SourceDateList;
  126. }else{
  127. filterDateList.clear();
  128. for(SortModel sortModel : SourceDateList){
  129. String name = sortModel.getName();
  130. if(name.indexOf(filterStr.toString()) != -1 || characterParser.getSelling(name).startsWith(filterStr.toString())){
  131. filterDateList.add(sortModel);
  132. }
  133. }
  134. }
  135. // 根据a-z进行排序
  136. Collections.sort(filterDateList, pinyinComparator);
  137. adapter.updateListView(filterDateList);
  138. }
  139. }

源码下载地址:http://download.csdn.net/detail/pcaxb/9116991



声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/105782
推荐阅读
相关标签
  

闽ICP备14008679号