listView 添加多个不同的adapter

本文介绍了一种在Android中实现ListView分类的方法,通过自定义SectionedAdapter来支持不同类型的视图和头部视图,使得列表可以按需显示一列、两列或多列内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

有时候我们想在listView上分类,或者呢 有时候一行显示两列内容,有时候需要三列内容 ,那怎么实现呢,这里呢就要使用

Java代码
  1. classSection{
  2. Stringcaption;
  3. Adapteradapter;
  4. Section(Stringcaption,Adapteradapter){
  5. this.caption=caption;
  6. this.adapter=adapter;
  7. }
  8. }

自定义一个类,这个类呢包含多个adapter就可以了,想用那种就用那种。

Java代码
  1. abstractpublicclassSectionedAdapterextendsBaseAdapter{
  2. abstractprotectedViewgetHeaderView(Stringcaption,intindex,ViewconvertView,ViewGroupparent);
  3. privateList<Section>sections=newArrayList<Section>();
  4. privatestaticintTYPE_SECTION_HEADER=0;
  5. publicSectionedAdapter(){
  6. super();
  7. }
  8. publicvoidaddSection(Stringcaption,Adapteradapter){
  9. sections.add(newSection(caption,adapter));
  10. }
  11. publicObjectgetItem(intposition){
  12. for(Sectionsection:this.sections){
  13. if(position==0){
  14. return(section);
  15. }
  16. intsize=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. publicintgetCount(){
  25. inttotal=0;
  26. for(Sectionsection:this.sections){
  27. total+=section.adapter.getCount()+1;//addoneforheader
  28. }
  29. return(total);
  30. }
  31. publicintgetViewTypeCount(){
  32. inttotal=1;//onefortheheader,plusthosefromsections
  33. for(Sectionsection:this.sections){
  34. total+=section.adapter.getViewTypeCount();
  35. }
  36. return(total);
  37. }
  38. publicintgetItemViewType(intposition){
  39. inttypeOffset=TYPE_SECTION_HEADER+1;//startcountingfromhere
  40. for(Sectionsection:this.sections){
  41. if(position==0){
  42. return(TYPE_SECTION_HEADER);
  43. }
  44. intsize=section.adapter.getCount()+1;
  45. if(position<size){
  46. return(typeOffset+section.adapter.getItemViewType(position-1));
  47. }
  48. position-=size;
  49. typeOffset+=section.adapter.getViewTypeCount();
  50. }
  51. return(-1);
  52. }
  53. publicbooleanareAllItemsSelectable(){
  54. return(false);
  55. }
  56. publicbooleanisEnabled(intposition){
  57. return(getItemViewType(position)!=TYPE_SECTION_HEADER);
  58. }
  59. @Override
  60. publicViewgetView(intposition,ViewconvertView,
  61. ViewGroupparent){
  62. intsectionIndex=0;
  63. for(Sectionsection:this.sections){
  64. if(position==0){
  65. return(getHeaderView(section.caption,sectionIndex,convertView,parent));
  66. }
  67. intsize=section.adapter.getCount()+1;
  68. if(position<size){
  69. return(section.adapter.getView(position-1,convertView,parent));
  70. }
  71. position-=size;
  72. sectionIndex++;
  73. }
  74. return(null);
  75. }
  76. @Override
  77. publiclonggetItemId(intposition){
  78. return(position);
  79. }
  80. classSection{
  81. Stringcaption;
  82. Adapteradapter;
  83. Section(Stringcaption,Adapteradapter){
  84. this.caption=caption;
  85. this.adapter=adapter;
  86. }
  87. }
  88. }

然后主类就是

Java代码
  1. publicclassSectionedDemoextendsListActivity{
  2. privatestaticString[]items={"lorem","ipsum","dolor","purus"};
  3. @Override
  4. publicvoidonCreate(Bundleicicle){
  5. super.onCreate(icicle);
  6. setContentView(R.layout.main);
  7. adapter.addSection("Original",newArrayAdapter<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",newArrayAdapter<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",newArrayAdapter<String>(this,
  18. android.R.layout.simple_list_item_1,
  19. list));
  20. setListAdapter(adapter);
  21. }
  22. SectionedAdapteradapter=newSectionedAdapter(){
  23. protectedViewgetHeaderView(Stringcaption,intindex,ViewconvertView,ViewGroupparent){
  24. TextViewresult=(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. }

其他的就需要你自己变化了,我这里只是吧内容打乱。

有些东西我只是简单调解没有源码或者我认为很简单的就不提供了。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值