Android imitation wechat address list sidebar effect

Let's first look at the rendering of Android imitation wechat address list sidebar

This is a common effect

The list is sorted according to the phonetic alphabet of the first character and can be located through the alphabetic index in the sidebar.

It is not difficult to achieve such an effect. Just customize an index view, and then introduce a jar package pinyin4j-2.5.0 that can parse Chinese characters

First, define the sidebar control view, just draw it directly.

The selected item of the letter will turn red, and the background will change color when sliding. At this time, the sidebar does not contain the centered prompt text

Sidebar just draws the sidebar index bar, and does not contain the prompt text in the middle. This can be added in another layout

Hintsidebar updates the text content and visibility of the centered textview through the callback interface

Layout used

At this point, the index view is drawn, but the positioning function needs to be completed through the callback interface.

After introducing the jar package, first set a tool class that contains a method that can parse strings, and the return value is the Pinyin initial corresponding to the first character or char type data containing a space.

Then define an entity class, including three attributes: user name, phone number, Pinyin initial of the first character of user name, etc. the comparable interface needs to be implemented for sorting

The main layout file is as follows

The contact list uses recyclerview, and an adapter needs to be defined

The following method is used to get the position of the first item with the first character sign in the contact list

public int getFirstPositionByChar(char sign)

The main activity code is as follows

Initdata() is used to populate the adapter with data

In this way, the whole effect is completed.

Code download is provided here: Android imitation wechat address list sidebar

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

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
分享
二维码
< <上一篇
下一篇>>