listView 添加多个不同的adapter

来源:互联网 发布:dota2 mac版下载 编辑:程序博客网 时间:2024/04/28 15:25

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

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

 

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

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

 

然后主类就是

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

 

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

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

原创粉丝点击