android高仿微信联系人选择

来源:互联网 发布:新三板智库 大数据 编辑:程序博客网 时间:2024/05/18 02:09
 

android高仿微信联系人选择

标签: android微信
 1899人阅读 评论(0) 收藏 举报
 分类:

这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述

如题,仿造微信联系人选择。

这里写图片描述

实现起来也是相当的简单。

这里写图片描述

ChineseToEnglish为网上找的中文转拼音的类, 
CompareSort是实现联系人list的排序类 
SideBarView右侧的字母条 
User UserAdapter 测试数据类

主要看下SideBarView

package com.jadyn.contactslist.contact;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Color;import android.graphics.Paint;import android.graphics.Typeface;import android.util.AttributeSet;import android.util.Log;import android.view.MotionEvent;import android.view.View;import com.jadyn.contactslist.R;/** * Created by Administrator on 2016/1/8. */public class SideBarView extends View{    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 selectPos = -1;    private final int defaultNormalColor = Color.TRANSPARENT;    private final int defaultPressColor = Color.parseColor("#1F000000");    private final int defaultTextSize = 30;    private final int defaultNorTextColor = Color.parseColor("#cc181818");    private final int defaultPressTextColor = Color.parseColor("#ff000000");    private int sideBarBgNorColor;    private int sideBarBgPressColor;    private int sideBarTextSize;    private int sideBarNorTextColor;    private int sideBarPressTextColor;    public SideBarView(Context context) {        this(context, null);    }    public SideBarView(Context context, AttributeSet attrs) {        this(context, attrs, 0);        init();    }    public SideBarView(Context context, AttributeSet attrs, int defStyleAttr) {        super(context, attrs, defStyleAttr);        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SideBarView, defStyleAttr, 0);        sideBarBgNorColor = typedArray.getColor(R.styleable.SideBarView_sidebar_nor_background,defaultNormalColor);        sideBarBgPressColor = typedArray.getColor(R.styleable.SideBarView_sidebar_press_background,defaultPressColor);        sideBarTextSize = typedArray.getInt(R.styleable.SideBarView_sidebar_text_size, defaultTextSize);        sideBarNorTextColor = typedArray.getColor(R.styleable.SideBarView_sidebar_text_color_nor, defaultNorTextColor);        sideBarPressTextColor = typedArray.getColor(R.styleable.SideBarView_sidebar_text_color_press, defaultPressTextColor);        typedArray.recycle();        init();    }    Paint paint;    Paint paintSelect;    private void init() {        paint= new Paint() ;        paint.setAntiAlias(true);        paint.setColor(sideBarNorTextColor);        paint.setTypeface(Typeface.DEFAULT_BOLD);        paint.setTextSize(sideBarTextSize);        paintSelect= new Paint() ;        paintSelect.setAntiAlias(true);        paintSelect.setTypeface(Typeface.DEFAULT_BOLD);        paintSelect.setTextSize(sideBarTextSize);        paintSelect.setColor(sideBarPressTextColor);    }    int height;    int width;    int perHeight;    @Override    public boolean onTouchEvent(MotionEvent event) {        float x = event.getY();        //计算点击位置所在的position        int position = (int) (x / perHeight);        switch (event.getAction()){            case MotionEvent.ACTION_DOWN:                //点击字母条变色                setBackgroundColor(sideBarBgPressColor);                //点击时回调                selectPos = position;                if(listener != null)                    listener.onLetterSelected(b[selectPos]);                invalidate();                break;            case MotionEvent.ACTION_MOVE:                //切换到其他字母时执行                if(position != selectPos){                    selectPos = position;                    if(listener != null)                        listener.onLetterChanged(b[selectPos]);                    invalidate();                }                break;            case MotionEvent.ACTION_UP:            case MotionEvent.ACTION_CANCEL:                setBackgroundColor(sideBarBgNorColor);                if(listener != null){                    listener.onLetterReleased(b[selectPos]);                }                break;        }        return true;    }    @Override    protected void onDraw(Canvas canvas) {        super.onDraw(canvas);        height = getHeight();        width = getWidth();        perHeight = height / b.length;        for (int i = 0; i < b.length; i++) {            //画字母            canvas.drawText(b[i],width/2 - paint.measureText(b[i])/2,perHeight * i+perHeight,paint);            //选中的字母变色            if(selectPos == i){                canvas.drawText(b[i],width/2 - paint.measureText(b[i])/2,perHeight * i+perHeight,paintSelect);            }        }    }    @Override    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {        int width = resolveMeasure(widthMeasureSpec, true);        int height = resolveMeasure(heightMeasureSpec,false);        setMeasuredDimension(width,height);    }    private int resolveMeasure(int measureSpec ,boolean isWidth) {        int result = 0 ;        int padding = isWidth ? getPaddingLeft() + getPaddingRight() : getPaddingTop() + getPaddingBottom();        // 获取宽度测量规格中的mode        int mode = MeasureSpec.getMode(measureSpec);        // 获取宽度测量规格中的size        int size = MeasureSpec.getSize(measureSpec);        switch (mode){            case MeasureSpec.EXACTLY:                result = size;                break;            case MeasureSpec.AT_MOST:                case MeasureSpec.UNSPECIFIED:                    float textWidth = paint.measureText(b[0]);                    if(isWidth){                        result = getSuggestedMinimumWidth() > textWidth ? getSuggestedMinimumWidth() : (int) textWidth;                        result += padding;                        result = Math.min(result,size);                    }else{                        result = size;                        result = Math.max(result,size);                    }                break;        }        return result;    }    public float dp2px(float dp) {        final float scale = getResources().getDisplayMetrics().density;        return dp * scale + 0.5f;    }    @Override    protected int getSuggestedMinimumWidth() {        return (int) dp2px(25);    }    public interface LetterSelectListener{        void onLetterSelected(String letter);        void onLetterChanged(String letter);        void onLetterReleased(String letter);    }    private LetterSelectListener listener;    public void setOnLetterSelectListen(LetterSelectListener listen){        this.listener = listen;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213

然后写一个排序的Comparator

//@标签代表A前面的那些,#代表除了A-Z以外的其他标签public class CompareSort implements Comparator<User> {    @Override    public int compare(User user1, User user2) {        if(user1.getLetter().equals("@") || user2.getLetter().equals("@")){            //通讯录前面的item(公众号,标签......)            return user1.getLetter().equals("@") ? -1:1;        }        //user1属于#标签,放到最后面        else if(!user1.getLetter().matches("[A-z]+")){            return 1;        //user2属于#标签,放到最后面        }else if(!user2.getLetter().matches("[A-z]+")){            return -1;        }else {            return user1.getLetter().compareTo(user2.getLetter());        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
public class UserAdapter extends BaseAdapter{    private Context mContext;    private ArrayList<User> users;    public UserAdapter(Context context) {        this.mContext = context;        users = new ArrayList<>();    }    public void setData(List<User> data){        this.users.clear();        this.users.addAll(data);    }    @Override    public int getCount() {        return users.size();    }    @Override    public Object getItem(int position) {        return users.get(position);    }    @Override    public long getItemId(int position) {        return 0;    }    @Override    public View getView(final int position, View convertView, ViewGroup parent) {        ViewHolder viewHolder = null;        if (convertView == null) {            viewHolder = new ViewHolder();            convertView = LayoutInflater.from(mContext).inflate(R.layout.item, null);            viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.title);            viewHolder.tvName = (TextView) convertView.findViewById(R.id.name);            viewHolder.tvItem = (LinearLayout) convertView.findViewById(R.id.item);            convertView.setTag(viewHolder);        } else {            viewHolder = (ViewHolder) convertView.getTag();        }        viewHolder.tvName.setText(users.get(position).getName());        viewHolder.tvItem.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Toast.makeText(mContext,users.get(position).getName(),Toast.LENGTH_SHORT).show();            }        });        //当前的item的title与上一个item的title不同的时候回显示title(A,B,C......)        if(position == getFirstLetterPosition(position) && !users.get(position).getLetter().equals("@")){            viewHolder.tvTitle.setVisibility(View.VISIBLE);            viewHolder.tvTitle.setText(users.get(position).getLetter().toUpperCase());        }else {            viewHolder.tvTitle.setVisibility(View.GONE);        }        return convertView;    }    /**     * 顺序遍历所有元素.找到position对应的title是什么(A,B,C?)然后找这个title下的第一个item对应的position     *     * @param position     * @return     */    private int getFirstLetterPosition(int position) {        String letter = users.get(position).getLetter();        int cnAscii = ChineseToEnglish.getCnAscii(letter.toUpperCase().charAt(0));        int size = users.size();        for (int i = 0; i < size; i++) {            if(cnAscii == users.get(i).getLetter().charAt(0)){                return i;            }        }        return -1;    }    /**     * 顺序遍历所有元素.找到letter下的第一个item对应的position     * @param letter     * @return     */    public int getFirstLetterPosition(String letter){        int size = users.size();        for (int i = 0; i < size; i++) {            if(letter.charAt(0) == users.get(i).getLetter().charAt(0)){                return i;            }        }        return -1;    }    class ViewHolder {        TextView tvName;        TextView tvTitle;        LinearLayout tvItem;    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
public class MainActivity extends ActionBarActivity implements SideBarView.LetterSelectListener {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        init();    }    ListView mListview;    UserAdapter mAdapter;    TextView mTip;    private void init() {        mListview = (ListView) findViewById(R.id.listview);        SideBarView sideBarView = (SideBarView) findViewById(R.id.sidebarview);        String[] contactsArray = getResources().getStringArray(R.array.data);        String[] headArray = getResources().getStringArray(R.array.head);        mTip = (TextView) findViewById(R.id.tip);        //模拟添加数据到Arraylist        int length = contactsArray.length;        ArrayList<User> users = new ArrayList<>();        for (int i = 0; i < length; i++) {            User user = new User();            user.setName(contactsArray[i]);            String firstSpell = ChineseToEnglish.getFirstSpell(contactsArray[i]);            String substring = firstSpell.substring(0, 1).toUpperCase();            if(substring.matches("[A-Z]")){                user.setLetter(substring);            }else {                user.setLetter("#");            }            users.add(user);        }        for (int i = 0; i < headArray.length; i++) {            User user = new User();            user.setName(headArray[i]);            user.setLetter("@");            users.add(user);        }        //排序        Collections.sort(users, new CompareSort());        //设置数据        mAdapter = new UserAdapter(this);        mAdapter.setData(users);        mListview.setAdapter(mAdapter);        //设置回调        sideBarView.setOnLetterSelectListen(this);    }    @Override    public void onLetterSelected(String letter) {        setListviewPosition(letter);        mTip.setText(letter);        mTip.setVisibility(View.VISIBLE);    }    @Override    public void onLetterChanged(String letter) {        setListviewPosition(letter);        mTip.setText(letter);    }    @Override    public void onLetterReleased(String letter) {        mTip.setVisibility(View.GONE);    }    private void setListviewPosition(String letter){        int firstLetterPosition = mAdapter.getFirstLetterPosition(letter);        if(firstLetterPosition != -1){            mListview.setSelection(firstLetterPosition);        }    }}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86

上面代码比较少,所以就懒得说了。有兴趣的可以下载来看看

这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述这里写图片描述

代码下载 
https://github.com/JadynChan/ContactsList 
http://download.csdn.net/detail/recall2012/9397297