Android custom view implementation letter navigation bar

Many Android entry program apes may be afraid of Android custom view, but this is the only way for experts to advance. They are ready to spend some time and write more articles on custom view.

Train of thought analysis:

1. Custom view to implement letter navigation bar

2. Listview implements the contact list

3. Letter navigation bar sliding event handling

4. Linkage between letter navigation bar and middle letter

5. Linkage between letter navigation bar and listview

design sketch:

First, let's throw out the main layout file to facilitate the description of the following code

Step 1: analyze the custom letter navigation bar

Train of thought analysis:

1. When using, we set the width to 20dp and the height to fill the parent control, so the width obtained here is 20dp

2. Through the cycle, draw the vertical letters. Each time we draw, we have to reset the color, because we need a selected letter. The color is different from the default

Step 2: the listview implements the contact list

Train of thought analysis:

1. In the main activity, define a data array, use the tool class to get the first letter of the array, and use collections to sort according to the first letter. Because the tool class is a little long, it will not be posted.

2. Create a listview sub layout and create an adapter for filling.

Main layout:

Listview sub layout:

Adapter:

Step 3: handle the sliding event of the letter navigation bar, and link the letter navigation bar with the middle letter

Train of thought analysis:

1. Override dispatchtouchevent in the custom view to handle the sliding event, and finally return true.

2. A textview is passed in from the main activity. Text is set when we slide and disappears when we release it. When setting text, you need to calculate the position of text, and if you slide too much, the array will cross the boundary, so we deal with the array cross-border problem in it.

3. Finally, an interface is provided to record the letters we slide to, so that they can be linked with listview later.

Step 4: linkage between letter navigation bar and listview

Train of thought analysis:

1. We have passed a selected letter through the interface and written the method to query position according to the letter in the adapter. At this time, as long as the main activity monitors and judges the user-defined view.

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