android格式化通讯录

//必须导包

 compile 'com.github.promeg:tinypinyin:2.0.1'
    // TinyPinyin 核心包,约 80KB
 compile 'com.github.promeg:tinypinyin-lexicons-android-cncity:2.0.1'


//MainActivity的布局

    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
            android:layout_weight="2"
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
            android:background="#ff0000"
        android:layout_weight="1"
        android:id="@+id/Side"
        android:layout_width="30dp"
        android:layout_height="match_parent" />

//适配器item的布局


    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
            android:textSize="40dp"
        android:id="@+id/tv_item"
        android:layout_width="match_parent"
        android:layout_height="50dp" />
            android:id="@+id/tv1_item"
        android:textSize="30dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

//MainAvtivity



public class MainActivity extends AppCompatActivity {
    private static final String[] LETTERS = new String[]{
            "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"
    };
    public static final String[] NAMES = new String[]{"宋江", "卢俊义", "吴用",
            "公孙胜", "关胜", "林冲", "秦明", "呼延灼", "花荣", "柴进", "李应", "朱仝", "鲁智深",
            "武松", "董平", "张清", "杨志", "徐宁", "索超", "戴宗", "刘唐", "李逵", "史进", "穆弘",
            "雷横", "李俊", "阮小二", "张横", "阮小五", " 张顺", "阮小七", "杨雄", "石秀", "解珍",
            " 解宝", "燕青", "朱武", "黄信", "孙立", "宣赞", "郝思文", "韩滔", "彭玘", "单廷珪",
            "魏定国", "萧让", "裴宣", "欧鹏", "邓飞", " 燕顺", "杨林", "凌振", "蒋敬", "吕方",
            "郭 盛", "安道全", "皇甫端", "王英", "扈三娘", "鲍旭", "樊瑞", "孔明", "孔亮", "项充",
            "李衮", "金大坚", "马麟", "童威", "童猛", "孟康", "侯健", "陈达", "杨春", "郑天寿",
            "陶宗旺", "宋清", "乐和", "龚旺", "丁得孙", "穆春", "曹正", "宋万", "杜迁", "薛永", "施恩",
            "周通", "李忠", "杜兴", "汤隆", "邹渊", "邹润", "朱富", "朱贵", "蔡福", "蔡庆", "李立",
            "李云", "焦挺", "石勇", "孙新", "顾大嫂", "张青", "孙二娘", " 王定六", "郁保四", "白胜",
            "时迁", "段景柱", "易宸锋"};
    private SidegView side;
    private ListView listview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//自定义的View
        side = (SidegView) findViewById(R.id.Side);
        listview = (ListView) findViewById(R.id.listview);
        final List list = new ArrayList<>();
        initData(list);
        listview.setAdapter(new MyBaseAdapter(list,this));
        side.suibianinterface(new SidegView.suibian() {
            @Override
            public void Data(String str) {
                for (int i = 0; i < list.size(); i++) {
                    String s = list.get(i).pinyin.charAt(0) + "";
                    if(TextUtils.equals(s,str)){
                        listview.setSelection(i);
                        break;
                    }
                }
            }
        });


    }


    private void initData(List list) {
        for (int i = 0; i < NAMES.length; i++) {


            list.add(new Bean(NAMES[i]));
        }
        Collections.sort(list);


    }
}

//自定义View的代码



public class SidegView extends View {
    private static final String[] LETTERS = new String[]{
            "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 measuredWidth;
    private float height;
    private Paint paint;


    public SidegView(Context context) {
        this(context,null);
    }


    public SidegView(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }


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


    private void initPaint() {
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTypeface(Typeface.DEFAULT_BOLD);
        paint.setTextSize(30);
        paint.setColor(Color.GREEN);
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                float y = event.getY();
                int i = (int) (y / height);
                if(i>=0&&i //                    Log.d("Letter", i + "下标");
                    String letter = LETTERS[i];
                    sui.Data(letter);


                }
                break;
            case MotionEvent.ACTION_MOVE:
                float y2 = event.getY();
                int i2 = (int) (y2 / height);
                if(i2>=0&&i2 //                    Log.d("Letter", i + "下标");
                    String letter = LETTERS[i2];
                    sui.Data(letter);
                }
                break;
        }


        return true;
    }


    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        for (int i = 0; i < LETTERS.length; i++) {
            float X = measuredWidth-(measuredWidth * 0.5f + paint.measureText(LETTERS[i]) * 0.5f);
            float Y = height * 0.5f + paint.measureText(LETTERS[i]) * 0.5f+i*height;
            canvas.drawText(LETTERS[i],X,Y,paint);
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int measuredHeight = getMeasuredHeight();
        measuredWidth = getMeasuredWidth();
        height = measuredHeight * 1.0f / LETTERS.length;
    }
    public void suibianinterface(suibian sui){
        this.sui=sui;
    }


    private suibian sui;


    interface suibian{
        void Data(String str);
    }
}

//保存首字母和name的Javabean

public class Bean implements Comparable{
    public String name;
    public String pinyin;


    public Bean(String name) {
        this.name = name;
        String pinyin = Pinyin.toPinyin(name, "");
        this.pinyin = pinyin.trim();
    }


    @Override
    public int compareTo(Bean bean) {
        return this.pinyin.compareTo(bean.pinyin);
    }
}

//适配器

public class MyBaseAdapter extends BaseAdapter {
    public List list_str;
    public Context context;


    public MyBaseAdapter(List list_str, Context context) {
        this.list_str = list_str;
        this.context = context;
    }


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


    @Override
    public Object getItem(int i) {
        return null;
    }


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


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        View inflate = View.inflate(context, R.layout.item, null);
        TextView text = inflate.findViewById(R.id.tv_item);
        TextView text1 = inflate.findViewById(R.id.tv1_item);
        String s = list_str.get(i).pinyin.charAt(0) + "";
        String index = null;
        if(i==0){
            index=s;
        }else{
            String s1 = list_str.get(i - 1).pinyin.charAt(0) + "";
            if(!TextUtils.equals(s,s1)){
            index=s;
            }
        }
        text.setVisibility(index!=null?View.VISIBLE:View.GONE);
        text.setText(index);
        text1.setText(list_str.get(i).name);
        return inflate;
    }
}

你可能感兴趣的:(android格式化通讯录)