6.我们来看看 是如何写的
/*** @Description:用来处理集合中数据的显示与排序* @author http://blog.csdn.net/finddreams*/public class SortAdapter extends BaseAdapter implements SectionIndexer {private List list = null;private Context mContext;public SortAdapter(Context mContext, List list) {this.mContext = mContext;this.list = list;}/*** 当ListView数据发生变化时,调用此方法来更新ListView* * @param list*/public void updateListView(List 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.phone_constacts_item, null);viewHolder.tvTitle = (TextView) view.findViewById(R.id.title);viewHolder.tvLetter = (TextView) view.findViewById(R.id.catalog);viewHolder.icon = (ImageTextView) view.findViewById(R.id.icon);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());viewHolder.icon.setText(this.list.get(position).getName());viewHolder.icon.setIconText(mContext, this.list.get(position).getName());return view;}final static class ViewHolder {TextView tvLetter;TextView tvTitle;ImageTextView icon;}/*** 根据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 "#";}}@Overridepublic Object[] getSections() {return null;}}
7.为了实现每个名字前面带有这个名字中第一个字的图片 , 我们首先要引入一个自定义的类 , 它是继承自的 , 我们所做的只是在拿到这个的Text内容 , 然后根据这个内容画出一张图片 , 具体代码如下:
/**
* @: 文字图片 , 这个相信大家都知道 , 比如QQ底部导航上的未读消息数
文章插图
* @
*/
class{
;
text;
d;
public ImageTextView(Context context) {super(context);}public ImageTextView(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);}public ImageTextView(Context context, AttributeSet attrs) {super(context, attrs);}public void setIconText(Context context, String text) {text = this.getText().toString().substring(0, 1);bitmap = BitmapUtil.getIndustry(context, text);d = BitmapUtil.bitmapTodrawable(bitmap);this.setCompoundDrawables(d, null, null, null);}
其中有两个方法:
/*** 根据文字获取图片* @param text* @return*/public static Bitmap getIndustry(Context context, String text) {String color = "#ffeeeade";Bitmap src = http://www.kingceram.com/post/BitmapFactory.decodeResource(context.getResources(),R.drawable.ic_launcher);int x = src.getWidth();int y = src.getHeight();Bitmap bmp = Bitmap.createBitmap(x, y, Bitmap.Config.ARGB_8888);Canvas canvasTemp = new Canvas(bmp);canvasTemp.drawColor(Color.parseColor(color));Paint p = new Paint(Paint.FAKE_BOLD_TEXT_FLAG | Paint.ANTI_ALIAS_FLAG);p.setColor(Color.parseColor("#ff4e0a13"));p.setAlpha(45);p.setFilterBitmap(true);int size = (int) (18 * context.getResources().getDisplayMetrics().density);p.setTextSize(size);float tX = (x - getFontlength(p, text)) / 2;float tY = (y - getFontHeight(p)) / 2 + getFontLeading(p);canvasTemp.drawText(text, tX, tY, p);return toRoundCorner(bmp, 2);}/*** @param bitmap* @return*/public static Drawable bitmapTodrawable(Bitmap bitmap) {Drawable drawable = new BitmapDrawable(bitmap);drawable.setBounds(0, 0, bitmap.getWidth(), bitmap.getHeight());return drawable;}
- 微信android架构图,Android 高通Camx架构学习 - 第1章
- Android设计模式之观察者模式
- 40个被世界遗忘的废墟之地,时间仿佛在这里停止… 世界十大废墟
- 仿膳月饼
- android自动黏贴功能,未来Android拥有拖拽复制粘贴功能
- android 复制粘贴监听
- 仿京东商城源码在哪里下载?
- linux环境android,图文详解在Linux下搭建Android开发环境
- android复制与粘贴
- android 群聊架构,[Android]搭建原生聊天架构