Imitation contact list or other letter Indexes

Source: Internet
Author: User
Tags getcolor

GitHub has many small functions. The requirements for different applications may be slightly different. For example, if the letters follow the slide when the listview slides, or if the letters are manually clicked, a prompt is displayed in the middle of the screen. There are also a variety of implementation ideas, such as draw each letter, and then process the slide; some may be to use textview to display the letter list. When you click an index, the background of the entire index is displayed, and the background disappears. Of course, these are all details. After reading a few demos, I feel that my current needs are somewhat different. In order to achieve "Big and complete", I decided to write one myself.

The requirement is as follows: the Chinese character participates in the index, and the letter or other item starting with the letter does not participate in the index, and is directly placed in the first "#". Click the letter index, and the color of the clicked letter list follows the change; the letter color of the manually sliding listview letter index also changes. You can easily click a letter to bring up the prompt. The final effect is as follows:


We decided to directly use the vertical linearlayout nested textview to display the letter list. The idea is as follows: Calculate the height of each letter based on the index height and add it to linearlayout. Then, process the touch event of linearlayout and obtain the coordinates to calculate which index should be indexed. The main problem encountered here is the grasp of textview, such as how to calculate its height change. When it is actually implemented, you will find that it has a paddingtop and paddingbottom. For other debugging methods, such as the sliding logic, you can directly run the Code:

Package COM. ttdevs. indicator. widget; import Java. util. list; import android. annotation. suppresslint; import android. content. context; import android. util. attributeset; import android. util. typedvalue; import android. view. gravity; import android. view. motionevent; import android. view. view; import android. widget. abslistview; import android. widget. abslistview. onscrolllistener; import android. widget. linearlayout; impo RT Android. widget. listview; import Android. widget. textview; import com. ttdevs. Indicator. R;/***** right letter index. <Br> * description: <br> * A line layout is nested in the line layout (to increase the area of the slide area, you can skip this step if you do not consider this ). <Br> * Insert each letter into an embedded linear layout. The size of each letter is calculated based on the height of the control. <Br> * usage: <br> * insert this view into your layout file, after initialization, set the data source for your listview and index (you need to organize and sort the data source yourself. <Br> * Organization of the Data source: if an item is not sorted, the data source is set to '0' or other ASCII characters smaller than 'A, all characters are converted to uppercase, so they must be sorted externally. <Br> * suggestion: If your data contains characters A0 and 20, we recommend that you remove them, for example: <br> * Str. replace ('',''); // A0-> 20 Str. replaceall ("", ""); <br> * if you are dealing with Chinese characters, you can use pinyin4j in libs to process ** @ author ttdevs 2014-07-31 **/public class extends linearlayout implements onscrolllistener {Private Static final string Indicator = "# attributes"; private linearlayout llmain; private listview mlistview; // Listviewprivate list <string> mdata; // data source private textview tvalert; // display the current letter private int mindex; // The indicator position private Boolean scrollable = true; // public listviewletterindicator (context) {This (context, null);} public listviewletterindicator (context, attributeset attrs) {super (context, attrs); setorientation (linearlayout. vertical); setbackgroundcolor (getresources (). getcolor (R. color. letter_indicator_background); // setpadding (8, 8, 8, 8); llmain = new linearlayout (getcontext (); llmain. setorientation (linearlayout. vertical); llmain. setgravity (gravity. center); // llmain. setpadding (2, 2, 2, 2); int width = (INT) getresources (). getdimension (R. dimen. letter_indicator_width); addview (llmain, width, linearlayout. layoutparams. match_parent);}/*** set the data source ** @ Param LV * The bound listview * @ Param data * data source for sorting */Public void setdata (listview LV, list <string> data) {setdata (LV, Data, null );} /***** set the data source ** @ Param LV * The listview bound * @ Param data * the data source for sorting * @ Param TV * shows the textview of the current letter */@ suppresslint ("defaultlocale ") public void setdata (listview LV, list <string> data, textview TV) {mlistview = lv; mdata = data; tvalert = TV; For (INT I = 0; I <mdata. size (); I ++) {string STR = mdata. get (I); mdata. set (I, str. touppercase ();} mlistview. setonscrolllistener (this); mindex = 0 ;}@ overrideprotected void onlayout (Boolean changed, int left, int top, int right, int bottom) {super. onlayout (changed, left, top, right, bottom); initview ();} private void initview () {int childcount = llmain. getchildcount (); If (childcount = indicator. length () {// llmain. invalidate (); return;} int Height = llmain. gethe Ight (); int textheight = (INT) math. floor (height/(indicator. length () + 6); linearlayout. layoutparams llptext = new linearlayout. layoutparams (linearlayout. layoutparams. wrap_content, linearlayout. layoutparams. wrap_content); For (INT I = 0; I <indicator. length (); I ++) {string STR = string. valueof (indicator. charat (I); textview tvindicator = new textview (getcontext (); tvindicator. settext (STR); tvindicat Or. setincludefontpadding (false); tvindicator. settextsize (typedvalue. complex_unit_px, textheight); tvindicator. settextcolor (getresources (). getcolor (R. color. letter_indicator_text_normal); // tvindicator. setpadding (0,-4, 0,-4); llmain. addview (tvindicator, llptext) ;}@ suppresslint ("defaultlocale") @ overridepublic Boolean dispatchtouchevent (motionevent eV) {int action = eV. getaction (); Switch (Action) {ca Se motionevent. action_down: Case motionevent. action_move: scrollable = false; float y = eV. gety (); float childy = 0; int Index = 0; For (INT I = 0; I <indicator. length (); I ++) {textview view = (textview) llmain. getchildat (I); childy = view. gettop (); int Height = view. getheight (); If (childy <Y & childy + height> Y) {Index = I; break;} view = NULL; // not neccessary} textview view = (textview) llmain. getch Ildat (indicator. length ()-1); If (Y> View. gettop () {Index = indicator. length ()-1 ;}view = NULL; changeindicatorcolor (INDEX); char indexindicator = indicator. charat (INDEX); // A: 65, #: 23If (indexindicator <'A') {mlistview. setselection (0);} else {for (INT I = 0; I <mdata. size (); I ++) {If (mdata. get (I ). charat (0)> = indexindicator) {mlistview. setselection (I); Return true ;}} break; Case motionevent. act Ion_up: postdelayed (New runnable () {@ overridepublic void run () {scrollable = true ;}}, 100); showtext ("", false); break; default: break;} return true;} @ overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {// invalidate () ;}@ suppresslint ("defaultlocale ") @ overridepublic void onscroll (abslistview view, int firstvisibleitem, int visibleitemcount, int totalitemcount) {try {If (! Scrollable | null = mdata | mdata. Size () = 0) {return;} If (null! = Mdata) {string STR = mdata. get (firstvisibleitem); char Indicator = Str. charat (0); If (indicator <'A') {changeindicatorcolor (0); Return ;}for (INT I = 1; I <indicator. length (); I ++) {If (indicator. charat (I) = indicator) {changeindicatorcolor (I); Return ;}}} catch (exception e) {e. printstacktrace () ;}} private void changeindicatorcolor (INT index) {If (mindex! = 0 & mindex = index) {return;} textview TV = (textview) llmain. getchildat (mindex); TV. settextcolor (getresources (). getcolor (R. color. letter_indicator_text_normal); TV = (textview) llmain. getchildat (INDEX); TV. settextcolor (getresources (). getcolor (R. color. letter_indicator_text_select); mindex = index; showtext (string. valueof (indicator. charat (mindex), true);} private void showtext (string text, Boolean is Show) {If (null! = Tvalert) {tvalert. settext (text); tvalert. setvisibility (isshow? View. visible: View. invisible) ;}// private drawable initbackground () {// float [] roundrect = new float [] {12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12}; // roundrectshape reoundrechshape = new roundrectshape (roundrect, null, // null); // shapedrawable drawable = new shapedrawable (reoundrechshape); // drawable. getpaint (). setcolor (color. parsecolor (gray_aa); // drawable. getpaint (). setstyle (paint. style. fill); // return drawable ;//}}

During the test, it is found that some items start with Chinese characters (you may need a toolkit for converting Chinese characters to PinYin at this time) but appear in "#". Check carefully, you will find that it is not a space. For details, see the code comment. Because it is very simple, I will not detail it in detail, just make a record. If you have any questions, leave a message to discuss it together. A test demo is provided: Download

Imitation contact list or other letter Indexes

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.