簡單通訊錄的實現

和氣的部落格發表於2017-09-25

匯入依賴,或者自行百度下載jar包 compile 'com.belerweb:pinyin4j:2.5.0'

public class CustomView 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"};
    Paint paint = new Paint();

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

    public CustomView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        //計算每一個字母的高度,總告訴除以字母集合的高度就可以
        int height = (getHeight()) / b.length;
        for (int i = 0; i < b.length; i++) {
            paint.setColor(Color.parseColor("#ff0000"));
            paint.setAntiAlias(true);//設定抗鋸齒
            paint.setTextSize(16);
            paint.setTypeface(Typeface.DEFAULT_BOLD);//設定字型加粗
            //計算每一個字母VIew的x軸座標
            float paintX = getWidth() / 2 - paint.measureText(b[i]) / 2;
            //計算每一個字母的高度
            int paintY = height * i + height;
            //繪畫出來這個TextView
            canvas.drawText(b[i], paintX, paintY, paint);
            //畫完一個以後重置畫筆
            paint.reset();
        }
    }


    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN://得到你當前點的導航欄的下標
                int down = (int) (event.getY() / getHeight() * b.length);
                if (down >= 0 && down < b.length) {
                    downData.DownData(b[down]);
                }
                break;
            case MotionEvent.ACTION_MOVE:
                int move = (int) (event.getY() / getHeight() * b.length);
                if (move >= 0 && move < b.length) {
                    downData.DownData(b[move]);
                }
                break;
        }
        return true;
    }

    private DownData downData;

    public void getDownData(DownData downData) {
        this.downData = downData;
    }

    public interface DownData {
        void DownData(String str);
    }
}
//MainActivity
public class MainActivity extends AppCompatActivity {
    String[] NAMES = new String[]{"宋江", "盧俊義", "吳用",
            "公孫勝", "關勝", "林沖", "秦明", "呼延灼", "花榮", "柴進", "李應", "朱仝", "魯智深",
            "武松", "董平", "張清", "楊志", "徐寧", "索超", "戴宗", "劉唐", "李逵", "史進", "穆弘",
            "雷橫", "李俊", "阮小二", "張橫", "阮小五", "張順", "阮小七", "楊雄", "石秀", "解珍",
            "解寶", "燕青", "朱武", "黃信", "孫立", "宣贊", "郝思文", "韓滔", "彭玘", "單廷珪",
            "魏定國", "蕭讓", "裴宣", "歐鵬", "鄧飛", "燕順", "楊林", "凌振", "蔣敬", "呂方",
            "郭 盛", "安道全", "皇甫端", "王英", "扈三娘", "鮑旭", "樊瑞", "孔明", "孔亮", "項充",
            "李袞", "金大堅", "馬麟", "童威", "童猛", "孟康", "侯健", "陳達", "楊春", "鄭天壽",
            "陶宗旺", "宋清", "樂和", "龔旺", "丁得孫", "穆春", "曹正", "宋萬", "杜遷", "薛永", "施恩",
            "周通", "李忠", "杜興", "湯隆", "鄒淵", "鄒潤", "朱富", "朱貴", "蔡福", "蔡慶", "李立",
            "李雲", "焦挺", "石勇", "孫新", "顧大嫂", "張青", "孫二孃", "王定六", "鬱保四", "白勝",
            "時遷", "段景柱", "易宸鋒"};
    private ListView listview;
    private CustomView custom;
    private ArrayList<person> list;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listview = (ListView) findViewById(R.id.listview);

        custom = (CustomView) findViewById(R.id.customView);
        list = new ArrayList<>();
        //
        for (int i = 0; i < NAMES.length; i++) {
            list.add(new person(NAMES[i]));
        }
        //設定介面卡
        listview.setAdapter(new MyAdaper(list, this));
        //對list集合中的首字母進行排序
        Collections.sort(list, new Comparator<person>() {
            @Override
            public int compare(person o1, person o2) {
                return o1.getHanderWord().compareTo(o2.getHanderWord());
            }
        });

        //自定義介面回撥,拿到你點選的拼音導航
        custom.getDownData(new CustomView.DownData() {
            @Override
            public void DownData(String str) {
                //迴圈判斷點選的拼音導航欄和集合中姓名的首字母,如果相同Listview就跳轉顯示
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).getHanderWord().equals(str)) {
                        listview.setSelection(i);
                        break;//此處記得一定要停止迴圈,否則會下標錯亂
                    }
                }
            }
        });
    }
}
//ListView的介面卡
public class MyAdaper extends BaseAdapter {
    private List<person> list;
    private Context context;

    public MyAdaper(List<person> list, Context context) {
        this.list = list;
        this.context = context;
    }

    @Override
    public int getCount() {
        return 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 convertView, ViewGroup parent) {
        ViewHolder holder = null;
        if (convertView == null) {
            convertView = View.inflate(context, R.layout.item, null);
            holder = new ViewHolder();
            holder.word = (TextView) convertView.findViewById(R.id.item_word);
            holder.name = (TextView) convertView.findViewById(R.id.item_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        String handerWord = list.get(position).getHanderWord();
        holder.word.setText(handerWord);//顯示首字母導航,A,B,C...
        holder.name.setText(list.get(position).getName());//顯示姓名
        //第一個字母顯示
        if (position == 0) {
            holder.word.setVisibility(View.VISIBLE);
        } else {
            //然後判斷當前姓名的首字母和上一個首字母是否相同,如果相同字母導航條就影藏,否則就顯示
            if (handerWord.equals(list.get(position - 1).getHanderWord())) {
                holder.word.setVisibility(View.GONE);
            } else {
                holder.word.setVisibility(View.VISIBLE);
            }
        }
        return convertView;
    }

    class ViewHolder {
        TextView word;
        TextView name;
    }
}
//用來儲存和得到姓名的拼音和拼音的首字母
public class person {
    private String name;
    private String pinyin;
    private String handerWord;

    public person(String name) {
        this.name = name;
        char c = name.charAt(0);//拿到姓名的第一個漢字
        this.pinyin = PinyinHelper.toHanyuPinyinStringArray(c)[0];//拿到名字的第一個漢字的拼音
        this.handerWord = pinyin.substring(0, 1).toUpperCase();//漢字拼音的首字母,變成大寫
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPinyin() {
        return pinyin;
    }

    public void setPinyin(String pinyin) {
        this.pinyin = pinyin;
    }

    public String getHanderWord() {
        return handerWord;
    }

    public void setHanderWord(String handerWord) {
        this.handerWord = handerWord;
    }
}
//MainActivity的xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"></ListView>

    <com.test.tong.CustomView
        android:id="@+id/customView"
        android:layout_width="30dp"
        android:layout_height="match_parent"
        android:layout_marginBottom="5dp"
        android:layout_marginTop="5dp" />

</LinearLayout>


相關文章