Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之ListView實現不同品種分類分隔欄的效果(非ExpandableListView實現)

Android開發之ListView實現不同品種分類分隔欄的效果(非ExpandableListView實現)

編輯:關於Android編程

我們有時候會遇到這麼一個情況。就是我在一個ListView裡面需要顯示的東西其實是有種類之分的。比如我要分冬天,夏天,秋天,春天,然後在這每個季節下面再去加載各自的條目數據。還有,比如我們的通訊錄,我們需要按A,B,C這樣的字母順序分類然後顯示。這個怎麼實現呢?

下面我們不用ExpandableListView,而是只用ListView來實現這一顯示效果。

 

\

 

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.  

             

             

             

             

             

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved