码迷,mamicode.com
首页 > 微信 > 详细

Android 仿微信联系人Demo

时间:2015-08-26 15:52:22      阅读:491      评论:0      收藏:0      [点我收藏+]

标签:android   微信   

技术分享

项目结构

技术分享

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ListView
            android:id="@+id/country_lvcountry"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_gravity="center"
            android:divider="@null" />

        <TextView
            android:id="@+id/dialog"
            android:layout_width="80.0dip"
            android:layout_height="80.0dip"
            android:layout_gravity="center"
            android:background="@drawable/corner_list_single_pressed"
            android:gravity="center"
            android:textColor="#ffffffff"
            android:textSize="30.0dip"
            android:visibility="invisible" />

        <com.example.contactdemo.SideBar
            android:id="@+id/sidrbar"
            android:layout_width="30.0dip"
            android:layout_height="fill_parent"
            android:layout_gravity="right|center" />
    </FrameLayout>

</LinearLayout>

item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:gravity="center_vertical"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/catalog"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_weight="1.0"
        android:background="#E0E0E0"
        android:paddingBottom="5dip"
        android:paddingLeft="5dip"
        android:paddingTop="5dip"
        android:text="A"
        android:textColor="#454545" />

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical"
        android:layout_marginLeft="5dip"
        android:layout_weight="1.0"
        android:gravity="center_vertical"
        android:paddingBottom="10dip"
        android:paddingTop="10dip"
        android:text="hhhh"
        android:textColor="#336598" />

</LinearLayout>

SideBar.java

package com.example.contactdemo;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.TextView;

public class SideBar extends View {

    private TextView textDialog;
    private OnSelectChangeListener changeListener;

    // 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();

    public SideBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    public SideBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public SideBar(Context context) {
        super(context);
    }

    public void setTextView(TextView textView) {
        this.textDialog = textView;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        int singleHeight = height / b.length;
        for (int i = 0; i < b.length; i++) {
            paint.setColor(Color.rgb(33, 65, 98));
            paint.setTypeface(Typeface.DEFAULT_BOLD);
            paint.setAntiAlias(true);
            paint.setTextSize(20);
            // 选中状态
            if (i == choose) {
                paint.setColor(Color.parseColor("#3399ff"));
                paint.setFakeBoldText(true);
            }
            float x = width / 2 - paint.measureText(b[i]) / 2;
            float y = singleHeight * i + singleHeight;
            canvas.drawText(b[i], x, y, paint);
            paint.reset();
        }
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent event) {
        int action = event.getAction();
        final float y = event.getY();// 点击y坐标
        int c = (int) (y / getHeight() * b.length);
        switch (action) {
        case MotionEvent.ACTION_UP:
            setBackgroundResource(R.color.alph);
            choose = -1;
            invalidate();
            if (textDialog != null)
                textDialog.setVisibility(View.INVISIBLE);
            break;

        default:
            setBackgroundColor(Color.parseColor("#28000000"));
            if (choose != c) {
                if (c >= 0 && c < b.length) {
                    if (changeListener != null)
                        changeListener.onSelectChange(b[c]);
                    if (textDialog != null) {
                        textDialog.setText(b[c]);
                        textDialog.setVisibility(View.VISIBLE);
                    }
                    choose = c;
                    invalidate();
                }
            }
            break;
        }
        return true;
    }

    public void setOnSelectChangeListener(OnSelectChangeListener listener) {
        this.changeListener = listener;
    }

    public interface OnSelectChangeListener {
        void onSelectChange(String letter);
    }
}

MyAdapter.java

package com.example.contactdemo;

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.TextView;

public class MyAdapter extends BaseAdapter {

    private List<MyModel> list = null;
    private Context mContext;

    public MyAdapter(List<MyModel> list, Context mContext) {
        this.list = list;
        this.mContext = mContext;
    }

    public void updateListView(List<MyModel> list) {
        this.list = list;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return this.list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder viewHolder = null;
        final MyModel mContent = list.get(position);
        if (view == null) {
            viewHolder = new ViewHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.item, 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 = getAsciiForPosition(position);

        // 如果当前位置等于该分类首字母的Char的位置 ,则认为是第一次出现
        if (position == getPositionFroAscii(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;
    }

    /**
     * 根据位置转成首字母的ASCII
     * 
     * @param position
     * @return
     */
    public int getAsciiForPosition(int position) {
        int a = list.get(position).getSortLetters().charAt(0);
        return a;
    }

    /**
     * 根据字母的ASCII获取位置
     * 
     * @param ascii
     * @return
     */
    public int getPositionFroAscii(int ascii) {
        for (int i = 0; i < list.size(); i++) {
            String abc = list.get(i).getSortLetters();
            char firstChar = abc.toUpperCase().charAt(0);
            if (firstChar == ascii)
                return i;
        }
        return -1;
    }

    class ViewHolder {
        TextView tvLetter;
        TextView tvTitle;
    }
}

MainActivity.java

package com.example.contactdemo;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.example.contactdemo.SideBar.OnSelectChangeListener;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {

    private SideBar sideBar;
    private TextView dialog;
    private ListView sortListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        sideBar = (SideBar) findViewById(R.id.sidrbar);
        dialog = (TextView) findViewById(R.id.dialog);
        sortListView = (ListView) findViewById(R.id.country_lvcountry);
        sideBar.setTextView(dialog);
        List<MyModel> datas = getDatas(getResources().getStringArray(R.array.data));
        Collections.sort(datas, new Comparator<MyModel>() {

            // -1代表o1里的某一个属性比o2的小 0代表等于 1代表大于
            @Override
            public int compare(MyModel o1, MyModel o2) {
                if (o1.getSortLetters().equals("#"))
                    return 1;
                else if (o2.getSortLetters().equals("#"))
                    return -1;
                else
                    return o1.getSortLetters().compareTo(o2.getSortLetters());
            }
        });
        final MyAdapter adapter = new MyAdapter(datas, this);
        sortListView.setAdapter(adapter);

        sideBar.setOnSelectChangeListener(new OnSelectChangeListener() {

            @Override
            public void onSelectChange(String letter) {
                int ascii = letter.charAt(0);
                int position = adapter.getPositionFroAscii(ascii);
                if(position!=-1)
                    sortListView.setSelection(position);
            }
        });
    }

    private List<MyModel> getDatas(String[] array) {
        List<MyModel> datas = new ArrayList<MyModel>();
        for (int i = 0; i < array.length; i++) {
            MyModel sortModel = new MyModel();
            sortModel.setName(array[i]);
            String abc = CharacterParser.getInstance().getSelling(array[i]);
            String sortString = abc.substring(0, 1).toUpperCase();
            // 正则表达式
            if (sortString.matches("[A-Z]")) {
                sortModel.setSortLetters(sortString);
            } else {
                sortModel.setSortLetters("#");
            }
            datas.add(sortModel);
        }
        return datas;
    }
}

源码下载

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android 仿微信联系人Demo

标签:android   微信   

原文地址:http://blog.csdn.net/coder_giser/article/details/48003479

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!