有時候我們想在listView上分類,或者呢 有時候一行顯示兩列內容,有時候需要三列內容 ,那怎么實現呢,這里呢就要使用
- class Section{
- Stringcaption;
- Adapteradapter;
- Section(Stringcaption,Adapteradapter){
- this .caption=caption;
- this .adapter=adapter;
- }
- }
自定義一個類,這個類呢包含多個adapter就可以了,想用那種就用那種。
- abstract public class SectionedAdapter extends BaseAdapter{
- abstract protected ViewgetHeaderView(Stringcaption, int index,ViewconvertView,ViewGroupparent);
- private List<Section>sections= new ArrayList<Section>();
- private static int TYPE_SECTION_HEADER= 0 ;
- public SectionedAdapter(){
- super ();
- }
- public void addSection(Stringcaption,Adapteradapter){
- sections.add( new Section(caption,adapter));
- }
- public ObjectgetItem( int position){
- for (Sectionsection: 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 (Sectionsection: this .sections){
- total+=section.adapter.getCount()+ 1 ; //addoneforheader
- }
- return (total);
- }
- public int getViewTypeCount(){
- int total= 1 ; //onefortheheader,plusthosefromsections
- for (Sectionsection: this .sections){
- total+=section.adapter.getViewTypeCount();
- }
- return (total);
- }
- public int getItemViewType( int position){
- int typeOffset=TYPE_SECTION_HEADER+ 1 ; //startcountingfromhere
- for (Sectionsection: 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 ViewgetView( int position,ViewconvertView,
- ViewGroupparent){
- int sectionIndex= 0 ;
- for (Sectionsection: 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{
- Stringcaption;
- Adapteradapter;
- Section(Stringcaption,Adapteradapter){
- this .caption=caption;
- this .adapter=adapter;
- }
- }
- }
然后主類就是
- public class SectionedDemo extends ListActivity{
- private static String[]items={ "lorem" , "ipsum" , "dolor" , "purus" };
- @Override
- public void onCreate(Bundleicicle){
- 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);
- }
- SectionedAdapteradapter= new SectionedAdapter(){
- protected ViewgetHeaderView(Stringcaption, int index,ViewconvertView,ViewGroupparent){
- TextViewresult=(TextView)convertView;
- if (convertView== null ){
- result=(TextView)getLayoutInflater().inflate(R.layout.header, null );
- }
- result.setText(caption);
- return (result);
- }
- };
- }
更多文章、技術交流、商務合作、聯系博主
微信掃碼或搜索:z360901061

微信掃一掃加我為好友
QQ號聯系: 360901061
您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。
【本文對您有幫助就好】元
