轉:listView 添加多個不同的adapter

有時候我們想在listView上分類,或者呢 有時候一行顯示兩列內容,有時候需要三列內容 ,那怎麼實現呢,這裏呢就要使用

Java代碼 複製代碼 收藏代碼

  1. class Section {    
  2.         String caption;    
  3.         Adapter adapter;    
  4.         Section(String caption, Adapter adapter) {    
  5. this.caption=caption;    
  6. this.adapter=adapter;    
  7.         }    
  8.     }   
class Section { String caption; Adapter adapter; Section(String caption, Adapter adapter) { this.caption=caption; this.adapter=adapter; } }

自定義一個類,這個類呢包含多個adapter就可以了,想用那種就用那種。

Java代碼 複製代碼 收藏代碼

  1. abstract public class SectionedAdapter extends BaseAdapter {    
  2. abstract protected View getHeaderView(String caption,int index,View convertView,ViewGroup parent);    
  3. private List<Section> sections=new ArrayList<Section>();    
  4. private static int TYPE_SECTION_HEADER=0;    
  5. public SectionedAdapter() {    
  6. super();    
  7.     }    
  8. public void addSection(String caption, Adapter adapter) {    
  9.         sections.add(new Section(caption, adapter));    
  10.     }    
  11. public Object getItem(int position) {    
  12. for (Section section : this.sections) {    
  13. if (position==0) {    
  14. return(section);    
  15.             }    
  16. int size=section.adapter.getCount()+1;    
  17. if (position<size) {    
  18. return(section.adapter.getItem(position-1));    
  19.             }    
  20.             position-=size;    
  21.         }    
  22. return(null);    
  23.     }    
  24. public int getCount() {    
  25. int total=0;    
  26. for (Section section : this.sections) {    
  27.             total+=section.adapter.getCount()+1; // add one for header  
  28.         }    
  29. return(total);    
  30.     }    
  31. public int getViewTypeCount() {    
  32. int total=1;    // one for the header, plus those from sections  
  33. for (Section section : this.sections) {    
  34.             total+=section.adapter.getViewTypeCount();    
  35.         }    
  36. return(total);    
  37.     }    
  38. public int getItemViewType(int position) {    
  39. int typeOffset=TYPE_SECTION_HEADER+1;   // start counting from here  
  40. for (Section section : this.sections) {    
  41. if (position==0) {    
  42. return(TYPE_SECTION_HEADER);    
  43.             }    
  44. int size=section.adapter.getCount()+1;    
  45. if (position&lt;size) {    
  46. return(typeOffset+section.adapter.getItemViewType(position-1));    
  47.             }    
  48.             position-=size;    
  49.             typeOffset+=section.adapter.getViewTypeCount();    
  50.         }    
  51. return(-1);    
  52.     }    
  53. public boolean areAllItemsSelectable() {    
  54. return(false);    
  55.     }    
  56. public boolean isEnabled(int position) {    
  57. return(getItemViewType(position)!=TYPE_SECTION_HEADER);    
  58.     }    
  59. @Override
  60. public View getView(int position, View convertView,    
  61.                                             ViewGroup parent) {    
  62. int sectionIndex=0;    
  63. for (Section section : this.sections) {    
  64. if (position==0) {    
  65. return(getHeaderView(section.caption, sectionIndex,convertView, parent));    
  66.             }    
  67. int size=section.adapter.getCount()+1;    
  68. if (position&lt;size) {    
  69. return(section.adapter.getView(position-1,  convertView,parent));    
  70.             }    
  71.             position-=size;    
  72.             sectionIndex++;    
  73.         }    
  74. return(null);    
  75.     }    
  76. @Override
  77. public long getItemId(int position) {    
  78. return(position);    
  79.     }    
  80. class Section {    
  81.         String caption;    
  82.         Adapter adapter;    
  83.         Section(String caption, Adapter adapter) {    
  84. this.caption=caption;    
  85. this.adapter=adapter;    
  86.         }    
  87.     }    
  88. }  
abstract public class SectionedAdapter extends BaseAdapter { abstract protected View getHeaderView(String caption,int index,View convertView,ViewGroup parent); private List&lt;Section> sections=new ArrayList<Section>(); private static int TYPE_SECTION_HEADER=0; public SectionedAdapter() { super(); } public void addSection(String caption, Adapter adapter) { sections.add(new Section(caption, adapter)); } public Object getItem(int position) { for (Section section : this.sections) { if (position==0) { return(section); } int size=section.adapter.getCount()+1; if (position<size) { return(section.adapter.getItem(position-1)); } position-=size; } return(null); } public int getCount() { int total=0; for (Section section : this.sections) { total+=section.adapter.getCount()+1; // add one for header } return(total); } public int getViewTypeCount() { int total=1; // one for the header, plus those from sections for (Section section : this.sections) { total+=section.adapter.getViewTypeCount(); } return(total); } public int getItemViewType(int position) { int typeOffset=TYPE_SECTION_HEADER+1; // start counting from here for (Section section : this.sections) { if (position==0) { return(TYPE_SECTION_HEADER); } int size=section.adapter.getCount()+1; if (position&lt;size) { return(typeOffset+section.adapter.getItemViewType(position-1)); } position-=size; typeOffset+=section.adapter.getViewTypeCount(); } return(-1); } public boolean areAllItemsSelectable() { return(false); } public boolean isEnabled(int position) { return(getItemViewType(position)!=TYPE_SECTION_HEADER); } @Override public View getView(int position, View convertView, ViewGroup parent) { int sectionIndex=0; for (Section section : this.sections) { if (position==0) { return(getHeaderView(section.caption, sectionIndex,convertView, parent)); } int size=section.adapter.getCount()+1; if (position&lt;size) { return(section.adapter.getView(position-1, convertView,parent)); } position-=size; sectionIndex++; } return(null); } @Override public long getItemId(int position) { return(position); } class Section { String caption; Adapter adapter; Section(String caption, Adapter adapter) { this.caption=caption; this.adapter=adapter; } } }

然後主類就是

Java代碼 複製代碼 收藏代碼

  1. public class SectionedDemo extends ListActivity {    
  2. private static String[] items={"lorem", "ipsum", "dolor","purus"};    
  3. @Override
  4. public void onCreate(Bundle icicle) {    
  5. super.onCreate(icicle);    
  6.         setContentView(R.layout.main);    
  7.         adapter.addSection("Original",new ArrayAdapter&lt;String>(this,    
  8.                                                     android.R.layout.simple_list_item_1,    
  9.                                                     items));    
  10.         List<String> list=Arrays.asList(items);    
  11.         Collections.shuffle(list);    
  12.         adapter.addSection("Shuffled",new ArrayAdapter<String>(this,    
  13.                                                     android.R.layout.simple_list_item_1,    
  14.                                                     list));    
  15.         list=Arrays.asList(items);    
  16.         Collections.shuffle(list);    
  17.         adapter.addSection("Re-shuffled",new ArrayAdapter<String>(this,    
  18.                                                     android.R.layout.simple_list_item_1,    
  19.                                                     list));    
  20.         setListAdapter(adapter);    
  21.     }    
  22.     SectionedAdapter adapter=new SectionedAdapter() {    
  23. protected View getHeaderView(String caption, int index,View convertView,ViewGroup parent) {    
  24.             TextView result=(TextView)convertView;    
  25. if (convertView==null) {    
  26.                 result=(TextView)getLayoutInflater().inflate(R.layout.header, null);    
  27.             }    
  28.             result.setText(caption);    
  29. return(result);    
  30.         }    
  31.     };    
  32. }  
public class SectionedDemo extends ListActivity { private static String[] items={"lorem", "ipsum", "dolor","purus"}; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); adapter.addSection("Original",new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items)); List<String> list=Arrays.asList(items); Collections.shuffle(list); adapter.addSection("Shuffled",new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list)); list=Arrays.asList(items); Collections.shuffle(list); adapter.addSection("Re-shuffled",new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list)); setListAdapter(adapter); } SectionedAdapter adapter=new SectionedAdapter() { protected View getHeaderView(String caption, int index,View convertView,ViewGroup parent) { TextView result=(TextView)convertView; if (convertView==null) { result=(TextView)getLayoutInflater().inflate(R.layout.header, null); } result.setText(caption); return(result); } }; }

其他的就需要你自己變化了,我這裏只是吧內容打亂。

有些東西我只是簡單調解沒有源碼或者我認爲很簡單的就不提供了。

對於複雜一些或者很難說清的 本人表達能力有限會把源碼發上的

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章