Android imitation wechat contacts are sorted alphabetically

As long as the app involves the contact interface, it is almost in alphabetical order and navigation bar. Since this demand is so hot, I began to learn relevant content. This article is independently compiled and summarized by referring to online materials. I hope it will help you more or less. If it is not well written, please give me some advice.

The renderings are as follows:

To achieve this effect, three knowledge points are needed: 1. Phonetic classification of strings; 2. Expandablelistview; 2. Secondary extended list; 3. Right letter classification view

Let's first understand the solutions one by one, and then write the code.

Implement letter classification:

Letter classification is divided into three small points: one is to convert Chinese into pinyin, the other is to sort according to the order of letters, and the other is that letters are only displayed in front of the first Chinese with the same initial.

1. To convert Chinese into pinyin, a toolkit, pinyin4j-2.5.0.jar, is used here. Official website address: http://pinyin4j.sourceforge.net/ Click download to import the project. (as for tutorials, there are many online)

Here we only need to use the code to convert Chinese into pinyin.

2. The implementation is sorted in alphabetical order. The comparator interface provided by Java is used. The first letter is obtained by using the previously obtained Chinese Pinyin, and the sorting is realized according to the ASCII value.

3. Letters are only displayed in front of the first Chinese with the same initial. This is a trick. The layout of letters and Chinese names are stored in the item layout of listview.

The layout of item is as follows:

Whether to display letters is determined by judging whether the position of the current item is equal to the index of the Chinese initial corresponding to the first item.

If they are equal, it means that they appear for the first time, and letters need to be displayed. Otherwise, letters will not be displayed. There is a premise for such a judgment, that is, the sorting of Chinese pinyin must be in alphabetical order, which is the necessity of sorting in the previous step. Realize the letter navigation on the right: the letter navigation on the right is essentially a user-defined view. In addition to drawing the interface, we should pay attention to the handling of touch events and the use of callback mechanism (which will be used in many places). This is more important. Go directly to the code.

Next is the code of mainactivity and sortadapter.

SortAdapter.java:

Although not complete, the more important codes have been posted. I hope they can help you

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>