【Android】仿微信通讯录中的右侧字母表控件

微信通讯录中的右侧有个字母条,通过它可以快速找到指定联系人,今天我仿照其样子写了一个

字母表控件代码

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import android.content.Context;  
  2. import android.graphics.Canvas;  
  3. import android.graphics.Color;  
  4. import android.graphics.Paint;  
  5. import android.util.AttributeSet;  
  6. import android.view.MotionEvent;  
  7. import android.view.View;  
  8.   
  9. import com.example.learn.R;  
  10.   
  11. /** 
  12.  *  
  13.  * Description: 仿微信通讯录中的右侧字母表控件 
  14.  *  
  15.  * @author danDingCongRong 
  16.  * @Version 1.0.0 
  17.  * @Created at 2014-5-31 22:16:15 
  18.  * @Modified by [作者] on [修改日期] 
  19.  */  
  20.   
  21. public class AlphabetView extends View {  
  22.   
  23.     // 字母表中的字符  
  24.     private String alphabet[] = { "@""A""B""C""D""E""F""G""H",  
  25.             "I""J""K""L""M""N""O""P""Q""R""S""T""U",  
  26.             "V""W""X""Y""Z""#", };  
  27.   
  28.     // 字母的颜色  
  29.     private int defaultColor = Color.RED;  
  30.     private int selectColor = Color.BLUE;  
  31.   
  32.     // 被选中的字符  
  33.     private int selectedIndex = 0;  
  34.   
  35.     // 画笔--用于绘制右侧字母  
  36.     Paint paint = new Paint();  
  37.   
  38.     // 选中的字母被改变监听器  
  39.     private OnTouchLetterChangedListener changedListener;  
  40.     // 选中的字母被释放监听器  
  41.     private OnTouchLetterReleasedListener releasedListener;  
  42.   
  43.     public AlphabetView(Context context) {  
  44.         super(context);  
  45.     }  
  46.   
  47.     public AlphabetView(Context context, AttributeSet attrs) {  
  48.         super(context, attrs);  
  49.     }  
  50.   
  51.     public AlphabetView(Context context, AttributeSet attrs, int defStyle) {  
  52.         super(context, attrs, defStyle);  
  53.     }  
  54.   
  55.     @Override  
  56.     protected void onDraw(Canvas canvas) {  
  57.         super.onDraw(canvas);  
  58.   
  59.         // 获取当前View的宽度和高度  
  60.         int width = getWidth();  
  61.         // 计算单个字符所占高度  
  62.         int singleLetter = getHeight() / (alphabet.length);  
  63.   
  64.         // 自上而下逐一绘制字母表中的每个字符  
  65.         for (int i = 0; i < alphabet.length; i++) {  
  66.             // 若没有没选中时显示默认颜色,若被选中显示指定的高亮色  
  67.             if (i != selectedIndex) {  
  68.                 paint.setColor(defaultColor);  
  69.                 paint.setAntiAlias(true);  
  70.                 paint.setTextSize(25);  
  71.             } else {  
  72.                 paint.setTextSize(25);  
  73.                 paint.setColor(selectColor);  
  74.             }  
  75.   
  76.             // 计算第i个字符在屏幕中的位置(x,y)  
  77.             float x = width / 2 - paint.measureText(alphabet[i]) / 2;  
  78.             float y = singleLetter * (i + 1);  
  79.   
  80.             // 在指定位置绘制指定字符  
  81.             canvas.drawText(alphabet[i], x, y, paint);  
  82.             // 重置画笔的属性  
  83.             paint.reset();  
  84.         }  
  85.     }  
  86.   
  87.     @Override  
  88.     public boolean dispatchTouchEvent(MotionEvent event) {  
  89.         // 被触摸的是字母表中的第几个字符  
  90.         int currentIndex = (int) (event.getY() / alphabet.length);  
  91.   
  92.         int action = event.getAction();  
  93.         switch (action) {  
  94.         case MotionEvent.ACTION_DOWN:  
  95.             // 监听到按下事件后修改被选中字符位置标识,并显示字母表的背景同时利用接口函数修改被选中字符  
  96.             if (currentIndex >= 0 && currentIndex < alphabet.length) {  
  97.                 selectedIndex = currentIndex;  
  98.                 changedListener  
  99.                         .onTouchLetterChangedListener(alphabet[currentIndex]);  
  100.                 setBackgroundResource(R.drawable.alpha_bg);  
  101.                 invalidate();  
  102.             }  
  103.             break;  
  104.         case MotionEvent.ACTION_MOVE:  
  105.             // 监听到正在滑动中的事件后修改被选中字符位置标识,利用接口函数修改被选中字符  
  106.             if (currentIndex >= 0 && currentIndex < alphabet.length) {  
  107.                 selectedIndex = currentIndex;  
  108.                 changedListener  
  109.                         .onTouchLetterChangedListener(alphabet[currentIndex]);  
  110.                 invalidate();  
  111.             }  
  112.             break;  
  113.         case MotionEvent.ACTION_UP:  
  114.             // 监听到弹起事件后,调用相应的监听事件并将字母表的背景设置为没有背景  
  115.             releasedListener.onTouchLetterReleasedListener();  
  116.             setBackgroundDrawable(null);  
  117.             invalidate();  
  118.             break;  
  119.         default:  
  120.             break;  
  121.         }  
  122.   
  123.         return true;  
  124.     }  
  125.   
  126.     // 设置字母表中的字符  
  127.     public void setAlphabet(String[] alphabet) {  
  128.         this.alphabet = alphabet;  
  129.     }  
  130.   
  131.     // 设置字母默认显示的颜色  
  132.     public void setDefaultColor(int defaultColor) {  
  133.         this.defaultColor = defaultColor;  
  134.     }  
  135.   
  136.     // 设置字母被选中时显示的颜色  
  137.     public void setSelectColor(int selectColor) {  
  138.         this.selectColor = selectColor;  
  139.     }  
  140.   
  141.     // 设置选中字母已改变监听事件  
  142.     public void setOnTouchLetterChangedListener(  
  143.             OnTouchLetterChangedListener changedListener) {  
  144.         this.changedListener = changedListener;  
  145.     }  
  146.   
  147.     // 设置已释放字母选中监听事件  
  148.     public void setOnTouchLetterReleasedListener(  
  149.             OnTouchLetterReleasedListener releasedListener) {  
  150.         this.releasedListener = releasedListener;  
  151.     }  
  152.   
  153.     public interface OnTouchLetterChangedListener {  
  154.   
  155.         public void onTouchLetterChangedListener(String s);  
  156.     }  
  157.   
  158.     public interface OnTouchLetterReleasedListener {  
  159.   
  160.         public void onTouchLetterReleasedListener();  
  161.     }  
  162.   
  163. }  

带字幕表的ListView及其中间字母组合控件

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. import java.util.HashMap;  
  2.   
  3. import android.content.Context;  
  4. import android.graphics.PixelFormat;  
  5. import android.graphics.drawable.Drawable;  
  6. import android.util.AttributeSet;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.WindowManager;  
  10. import android.widget.AdapterView;  
  11. import android.widget.AdapterView.OnItemClickListener;  
  12. import android.widget.BaseAdapter;  
  13. import android.widget.ListView;  
  14. import android.widget.RelativeLayout;  
  15. import android.widget.TextView;  
  16.   
  17. import com.example.learn.R;  
  18. import com.example.learn.overall.view.AlphabetView.OnTouchLetterChangedListener;  
  19. import com.example.learn.overall.view.AlphabetView.OnTouchLetterReleasedListener;  
  20.   
  21. public class AlphabetListView extends RelativeLayout {  
  22.   
  23.     // 字母表中的字母与ListView中各元素位置间的映射关系  
  24.     private HashMap<String, Integer> alphaIndexMap;  
  25.   
  26.     private ListView listView;  
  27.     private TextView tvCenterLetter;  
  28.     private AlphabetView alphabetView;  
  29.   
  30.     private WindowManager windowManager;  
  31.   
  32.     public AlphabetListView(Context context) {  
  33.         super(context);  
  34.   
  35.         initView(context);  
  36.     }  
  37.   
  38.     public AlphabetListView(Context context, AttributeSet attrs) {  
  39.         super(context, attrs);  
  40.   
  41.         initView(context);  
  42.     }  
  43.   
  44.     public AlphabetListView(Context context, AttributeSet attrs, int defStyle) {  
  45.         super(context, attrs, defStyle);  
  46.   
  47.         initView(context);  
  48.     }  
  49.   
  50.     private void initView(Context context) {  
  51.         initCenterLetterHit(context);  
  52.         LayoutInflater.from(context).inflate(R.layout.alphabet_listview, this,  
  53.                 true);  
  54.         listView = (ListView) findViewById(R.id.customListView);  
  55.         alphabetView = (AlphabetView) findViewById(R.id.alphabetView);  
  56.   
  57.         alphabetView  
  58.                 .setOnTouchLetterChangedListener(new OnTouchLetterChangedListener() {  
  59.   
  60.                     @Override  
  61.                     public void onTouchLetterChangedListener(String s) {  
  62.                         tvCenterLetter.setText(s);  
  63.                         tvCenterLetter.setVisibility(View.VISIBLE);  
  64.   
  65.                         if (null != alphaIndexMap  
  66.                                 && alphaIndexMap.containsKey(s)) {  
  67.                             Integer alphaIndex = alphaIndexMap.get(s);  
  68.                             listView.setSelection(alphaIndex);  
  69.                         }  
  70.                     }  
  71.                 });  
  72.   
  73.         alphabetView  
  74.                 .setOnTouchLetterReleasedListener(new OnTouchLetterReleasedListener() {  
  75.   
  76.                     @Override  
  77.                     public void onTouchLetterReleasedListener() {  
  78.                         tvCenterLetter.setVisibility(View.GONE);  
  79.                     }  
  80.                 });  
  81.     }  
  82.   
  83.     // 初始化位于字母中央的字母提示  
  84.     private void initCenterLetterHit(Context context) {  
  85.         View centerLetterView = LayoutInflater.from(context).inflate(  
  86.                 R.layout.center_letter, null);  
  87.         tvCenterLetter = (TextView) centerLetterView  
  88.                 .findViewById(R.id.tvCenterLetter);  
  89.         tvCenterLetter.setVisibility(View.INVISIBLE);  
  90.   
  91.         final WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(  
  92.                 LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT,  
  93.                 WindowManager.LayoutParams.TYPE_APPLICATION,  
  94.                 WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE  
  95.                         | WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE,  
  96.                 PixelFormat.TRANSLUCENT);  
  97.   
  98.         windowManager = (WindowManager) context  
  99.                 .getSystemService(Context.WINDOW_SERVICE);  
  100.         windowManager.addView(centerLetterView, layoutParams);  
  101.     }  
  102.   
  103.     public void setAlphaIndexMap(HashMap<String, Integer> alphaIndexMap) {  
  104.         this.alphaIndexMap = alphaIndexMap;  
  105.     }  
  106.   
  107.     public void setAdapter(BaseAdapter adapter) {  
  108.         listView.setAdapter(adapter);  
  109.     }  
  110.   
  111.     public void setOnItemClickListener(  
  112.             final OnItemClickListener onItemClickListener) {  
  113.         if (null != onItemClickListener) {  
  114.             listView.setOnItemClickListener(new ListView.OnItemClickListener() {  
  115.   
  116.                 @Override  
  117.                 public void onItemClick(AdapterView<?> parent,  
  118.                         View convertView, int position, long id) {  
  119.                     onItemClickListener.onItemClick(parent, convertView,  
  120.                             position, id);  
  121.                 }  
  122.             });  
  123.         }  
  124.     }  
  125.   
  126.     public void setOnItemLongClickListenter(  
  127.             final OnItemLongClickListener onItemLongClickListener) {  
  128.         if (null != onItemLongClickListener) {  
  129.             listView.setOnItemLongClickListener(new ListView.OnItemLongClickListener() {  
  130.   
  131.                 @Override  
  132.                 public boolean onItemLongClick(AdapterView<?> parent,  
  133.                         View convertView, int position, long id) {  
  134.                     onItemLongClickListener.onItemLongClick(parent,  
  135.                             convertView, position, id);  
  136.   
  137.                     return false;  
  138.                 }  
  139.             });  
  140.         }  
  141.     }  
  142.   
  143.     public void setBackgroundDrawable(Drawable background) {  
  144.         listView.setBackgroundDrawable(background);  
  145.     }  
  146.   
  147.     public void setBackResource(int resourceId) {  
  148.         listView.setBackgroundResource(resourceId);  
  149.     }  
  150.   
  151.     public void setAlphabetDefaultColor(int defaultColor) {  
  152.         alphabetView.setDefaultColor(defaultColor);  
  153.     }  
  154.   
  155.     public void setAlphabetSelectedColor(int selectColor) {  
  156.         alphabetView.setSelectColor(selectColor);  
  157.     }  
  158.   
  159.     public void setDivider(int height) {  
  160.         listView.setDividerHeight(height);  
  161.     }  
  162.   
  163.     public void setCenterLetterColor(int color) {  
  164.         tvCenterLetter.setTextColor(color);  
  165.     }  
  166.   
  167.     public void setCenterLetterBackgroundResource(int resourceId) {  
  168.         tvCenterLetter.setBackgroundResource(resourceId);  
  169.     }  
  170.   
  171.     public void setCenterLetterTextSize(float size) {  
  172.         tvCenterLetter.setTextSize(size);  
  173.     }  
  174.   
  175.     public interface OnItemClickListenter {  
  176.   
  177.         public void onItemClick(AdapterView<?> parent, View convertView,  
  178.                 int position, long id);  
  179.     }  
  180.   
  181.     public interface OnItemLongClickListener {  
  182.   
  183.         public boolean onItemLongClick(AdapterView<?> parent, View convertView,  
  184.                 int position, long id);  
  185.     }  
  186.   
  187. }  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值