有时候我们想在listView上分类,或者呢 有时候一行显示两列内容,有时候需要三列内容 ,那怎么实现呢,这里呢就要使用
- classSection{
- Stringcaption;
- Adapteradapter;
- Section(Stringcaption,Adapteradapter){
- this.caption=caption;
- this.adapter=adapter;
- }
- }
自定义一个类,这个类呢包含多个adapter就可以了,想用那种就用那种。
- abstractpublicclassSectionedAdapterextendsBaseAdapter{
- abstractprotectedViewgetHeaderView(Stringcaption,intindex,ViewconvertView,ViewGroupparent);
- privateList<Section>sections=newArrayList<Section>();
- privatestaticintTYPE_SECTION_HEADER=0;
- publicSectionedAdapter(){
- super();
- }
- publicvoidaddSection(Stringcaption,Adapteradapter){
- sections.add(newSection(caption,adapter));
- }
- publicObjectgetItem(intposition){
- for(Sectionsection:this.sections){
- if(position==0){
- return(section);
- }
- intsize=section.adapter.getCount()+1;
- if(position<size){
- return(section.adapter.getItem(position-1));
- }
- position-=size;
- }
- return(null);
- }
- publicintgetCount(){
- inttotal=0;
- for(Sectionsection:this.sections){
- total+=section.adapter.getCount()+1;//addoneforheader
- }
- return(total);
- }
- publicintgetViewTypeCount(){
- inttotal=1;//onefortheheader,plusthosefromsections
- for(Sectionsection:this.sections){
- total+=section.adapter.getViewTypeCount();
- }
- return(total);
- }
- publicintgetItemViewType(intposition){
- inttypeOffset=TYPE_SECTION_HEADER+1;//startcountingfromhere
- for(Sectionsection:this.sections){
- if(position==0){
- return(TYPE_SECTION_HEADER);
- }
- intsize=section.adapter.getCount()+1;
- if(position<size){
- return(typeOffset+section.adapter.getItemViewType(position-1));
- }
- position-=size;
- typeOffset+=section.adapter.getViewTypeCount();
- }
- return(-1);
- }
- publicbooleanareAllItemsSelectable(){
- return(false);
- }
- publicbooleanisEnabled(intposition){
- return(getItemViewType(position)!=TYPE_SECTION_HEADER);
- }
- @Override
- publicViewgetView(intposition,ViewconvertView,
- ViewGroupparent){
- intsectionIndex=0;
- for(Sectionsection:this.sections){
- if(position==0){
- return(getHeaderView(section.caption,sectionIndex,convertView,parent));
- }
- intsize=section.adapter.getCount()+1;
- if(position<size){
- return(section.adapter.getView(position-1,convertView,parent));
- }
- position-=size;
- sectionIndex++;
- }
- return(null);
- }
- @Override
- publiclonggetItemId(intposition){
- return(position);
- }
- classSection{
- Stringcaption;
- Adapteradapter;
- Section(Stringcaption,Adapteradapter){
- this.caption=caption;
- this.adapter=adapter;
- }
- }
- }
然后主类就是
- publicclassSectionedDemoextendsListActivity{
- privatestaticString[]items={"lorem","ipsum","dolor","purus"};
- @Override
- publicvoidonCreate(Bundleicicle){
- super.onCreate(icicle);
- setContentView(R.layout.main);
- adapter.addSection("Original",newArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1,
- items));
- List<String>list=Arrays.asList(items);
- Collections.shuffle(list);
- adapter.addSection("Shuffled",newArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1,
- list));
- list=Arrays.asList(items);
- Collections.shuffle(list);
- adapter.addSection("Re-shuffled",newArrayAdapter<String>(this,
- android.R.layout.simple_list_item_1,
- list));
- setListAdapter(adapter);
- }
- SectionedAdapteradapter=newSectionedAdapter(){
- protectedViewgetHeaderView(Stringcaption,intindex,ViewconvertView,ViewGroupparent){
- TextViewresult=(TextView)convertView;
- if(convertView==null){
- result=(TextView)getLayoutInflater().inflate(R.layout.header,null);
- }
- result.setText(caption);
- return(result);
- }
- };
- }
其他的就需要你自己变化了,我这里只是吧内容打乱。
有些东西我只是简单调解没有源码或者我认为很简单的就不提供了。