簡單通訊錄的實現
匯入依賴,或者自行百度下載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>
相關文章
- 完整版通訊錄(實現簡單具體易上手!!)
- c#實現最簡單的socket通訊C#
- 【練手小專案】簡易通訊錄:單連結串列實現
- 簡單的Java實現Netty進行通訊JavaNetty
- 智慧家居簡單實現---使用ESP8266簡單實現和APP通訊APP
- C# 如何實現簡單的Socket通訊(附示例)C#
- 利用SQL實現通訊錄SQL
- 呼叫通訊錄實現思路
- 透過資料結構實現簡易通訊錄資料結構
- 使用go net實現簡單的redis通訊協議YWSVGoRedis協議
- 簡單的Socket通訊
- TCP和UDP實現簡單一對一通訊TCPUDP
- socket 完成簡單的通訊
- websocket+node實現一個最簡單的即時通訊功能Web
- 網路通訊2:TCP簡單通訊TCP
- LayoutTransiton實現簡單的錄製按鈕
- Dubbo的簡單使用以及Triple協議的Streaming通訊的實現協議
- TCP通訊客戶端和服務端簡單程式碼實現TCP客戶端服務端
- 通訊錄管理系統(C++實現)C++
- 怎麼實現通訊錄字母表
- 簡單區分WiFi通訊和WLAN通訊WiFi
- Go語言實現的簡易TCP通訊框架GoTCP框架
- scrollIntoView()實現通訊錄功能-錨點定位View
- SwiftNIO初探-簡單UDP通訊SwiftUDP
- Java的Socket通訊簡單例項Java單例
- smack和openfire通訊的簡單例子Mac單例
- 簡單的區域網內通訊
- Redis實現簡單訊息佇列Redis佇列
- miniSipServer簡單而不簡單,輕鬆落地,實現電腦對固話、手機通訊Server
- 用JS實現簡單的螢幕錄影機JS
- 教你C語言實現通訊錄的詳細程式碼C語言
- 【練習】製作簡易通訊錄
- 一個簡單的netty通訊的例子Netty
- 造輪子系列(二): 史上最簡單的長連線通訊協議及實現協議
- Java review--NIO例項:實現服務端和客戶端的簡單通訊JavaView服務端客戶端
- 網路通訊2:TCP通訊實現TCP
- Android實現錄屏直播(一)ScreenRecorder的簡單分析Android
- css模擬實現手機通訊錄佈局效果CSS