listView 添加多个不同的adapter

本来想自己写的,但是觉得自己表达能力还不够,ListView添加不同的adapter又比较麻烦,还是直接转载人家大神的大作吧,当然,我会在排版上做一下处理,让看起来更清晰点,因为我原来找到的文章,让我看的眼睛都快不要了。。。

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

class Section {   
        String caption;   
        Adapter adapter;   
           
        Section(String caption, Adapter adapter) {   
            this.caption=caption;   
            this.adapter=adapter;   
        }   
    }   
 


 

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

abstract public class SectionedAdapter extends BaseAdapter {   
    abstract protected View getHeaderView(String caption,int index,View convertView,ViewGroup parent);   
       
    private List<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<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<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代码 

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);   
        }   
    };   
}  

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

作者“zhujianjia”
这里说明一下,作者已经没办法找了,我只能在我原来看到文章的地方看到了这个作者信息,向原作者表示致敬。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值