listView添加多個不同的adapter – Android移動開發技術文章_手機開發 Android移動開發教學課程

有時候我們想在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”
 

發佈留言