Using ListView in Android development to implement the effect of different categories of separation columns (non-ExpandableListView implementation)

Source: Internet
Author: User

Using ListView in Android development to implement the effect of different categories of separation columns (non-ExpandableListView implementation)

We sometimes encounter such a situation. There are actually different types of things that I need to display in a ListView. For example, I want to load data entries in Winter, summer, autumn, and spring. Also, for example, in our address book, we need to classify and display the address book in alphabetical order like A, B, and C. How can this be achieved?

Next, we don't need to use ExpandableListView, but only use ListView to achieve this display effect.

 

 

MainActivity. java [Java]
  1. Package com. xzq. listviewadapter;
  2.  
  3. Import android. app. Activity;
  4. Import android. OS. Bundle;
  5. Import android. view. Window;
  6. Import android. widget. ListView;
  7.  
  8. Public class MainActivity extends Activity {
  9.  
  10. Private String [] string = {A, B, C, D, E, F, G, H, I,
  11. J, K, L, M, N, O, P, Q, R, S, T, U, V,
  12. W, X, Y, Z };
  13.  
  14. /** Called when the activity is first created .*/
  15. @ Override
  16. Public void onCreate (Bundle savedInstanceState ){
  17. Super. onCreate (savedInstanceState );
  18. RequestWindowFeature (Window. FEATURE_NO_TITLE );
  19. SetContentView (R. layout. main );
  20.  
  21. ListView listview = (ListView) findViewById (R. id. listview );
  22. MyCustomAdapter adapter = new MyCustomAdapter (this );
  23.  
  24. Int size = string. length;
  25. For (int I = 0; I <size; I ++ ){
  26. Adapter. addSeparatorItem (string [I]);
  27. For (int k = 0; k <5; k ++ ){
  28. Adapter. addItem (item + k );
  29. }
  30. }
  31. Listview. setAdapter (adapter );
  32. }
  33. }
    MyCustomAdapter. java[Java]
    1. Package com. xzq. listviewadapter;
    2.  
    3. Import java. util. ArrayList;
    4. Import java. util. TreeSet;
    5.  
    6. Import android. content. Context;
    7. Import android. view. LayoutInflater;
    8. Import android. view. View;
    9. Import android. view. ViewGroup;
    10. Import android. widget. BaseAdapter;
    11. Import android. widget. TextView;
    12.  
    13. Public class MyCustomAdapter extends BaseAdapter {
    14.  
    15. Private static final int TYPE_ITEM = 0;
    16. Private static final int TYPE_SEPARATOR = 1;
    17. Private static final int TYPE_MAX_COUNT = TYPE_SEPARATOR + 1;
    18. Private ArrayList Data = new ArrayList ();
    19. Private LayoutInflater inflater;
    20. Private TreeSet Set = new TreeSet ();
    21.  
    22. Public MyCustomAdapter (Context context ){
    23. Inflater = LayoutInflater. from (context );
    24. }
    25.  
    26. Public void addItem (String item ){
    27. Data. add (item );
    28. }
    29.  
    30. Public void addSeparatorItem (String item ){
    31. Data. add (item );
    32. Set. add (data. size ()-1 );
    33. }
    34.  
    35. Public int getItemViewType (int position ){
    36. Return set. contains (position )? TYPE_SEPARATOR: TYPE_ITEM;
    37. }
    38.  
    39. @ Override
    40. Public int getViewTypeCount (){
    41. Return TYPE_MAX_COUNT;
    42. }
    43.  
    44. @ Override
    45. Public int getCount (){
    46. Return data. size ();
    47. }
    48.  
    49. @ Override
    50. Public Object getItem (int position ){
    51. Return data. get (position );
    52. }
    53.  
    54. @ Override
    55. Public long getItemId (int position ){
    56. Return position;
    57. }
    58.  
    59. @ Override
    60. Public View getView (int position, View convertView, ViewGroup parent ){
    61. ViewHolder holder = null;
    62. Int type = getItemViewType (position );
    63. If (convertView = null ){
    64. Holder = new ViewHolder ();
    65. Switch (type ){
    66. Case TYPE_ITEM:
    67. ConvertView = inflater. inflate (R. layout. item1, null );
    68. Holder. textView = (TextView) convertView
    69. . FindViewById (R. id. item1 );
    70. Break;
    71. Case TYPE_SEPARATOR:
    72. ConvertView = inflater. inflate (R. layout. item2, null );
    73. Holder. textView = (TextView) convertView
    74. . FindViewById (R. id. item2 );
    75. Break;
    76. }
    77. ConvertView. setTag (holder );
    78. } Else {
    79. Holder = (ViewHolder) convertView. getTag ();
    80. }
    81. Holder. textView. setText (data. get (position ));
    82. Return convertView;
    83. }
    84.  
    85. Public static class ViewHolder {
    86. Public TextView textView;
    87. }
    88. }
      Main. xml[Html]
      1. Android: layout_width = fill_parent
      2. Android: layout_height = fill_parent
      3. Android: background = # ffffff
      4. Android: orientation = vertical>
      5.  
      6. Android: id = @ + id/listview
      7. Android: layout_width = fill_parent
      8. Android: layout_height = wrap_content
      9. Android: text = @ string/hello/>
      10.  


      11. Item1.xml[Html] 
        1. Android: layout_width = fill_parent
        2. Android: layout_height = fill_parent
        3. Android: background = # ffffff
        4. Android: orientation = vertical>
        5.  
        6. Android: id = @ + id/item1
        7. Android: layout_width = fill_parent
        8. Android: layout_height = 30dip
        9. Android: gravity = center_vertical/>
        10.  


        11. Item2.xml[Html] 
          1. Android: layout_width = fill_parent
          2. Android: layout_height = fill_parent
          3. Android: orientation = vertical>
          4.  
          5. Android: id = @ + id/item2
          6. Android: layout_width = fill_parent
          7. Android: layout_height = 30dip
          8. Android: background = #3c4857
          9. Android: gravity = center_vertical/>
          10.  


          11.  

             

             

             

             

             

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.