2012-06-25 26 views
6

我爲我的ListView定製了一個適配器我想將項目名稱添加爲我的工作請求的標題。添加一個頭文件很好,但我不確定如何使用addHeaderView添加多個頭文件。我不明白在哪裏放置setAdapter還是應該放置多次?如何在ListView中添加多個標題視圖

這是我的一個頭java代碼其中工程:

mListView = (ListView)findViewById(R.id.dashboardList); 
View header1 = getLayoutInflater().inflate(R.layout.listview_header, null, false); 
tv = (TextView) header1.findViewById(R.id.listHeader); 
adapter = new MyCustomAdapter(MyDashboardActivity.this, R.layout.mydashboard_row, dashboardBean); 
tv.setText("Project 1"); 
mListView.addHeaderView(header1, null, false); 
for (int i=0; i < 7; i++) { 
    dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i])); 
} 
mListView.setAdapter(adapter); 

現在,我有兩個頭我嘗試這樣做:

mListView = (ListView)findViewById(R.id.dashboardList); 
View header1 = getLayoutInflater().inflate(R.layout.listview_header, null, false); 
tv = (TextView) header1.findViewById(R.id.listHeader); 
adapter = new MyCustomAdapter(MyDashboardActivity.this, R.layout.mydashboard_row, dashboardBean); 
tv.setText("RxOffice"); 
mListView.addHeaderView(header1, null, false); 
for (int i=0; i < 4; i++) { 
    dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i])); 
} 

tv.setText(Project 2"); 

mListView.addHeaderView(header1, null, false); 
for (int i=4; i < workRequests.length; i++) { 
    dashboardBean.add(new DashboardBean(workRequests[i],status[i],actualHours[i])); 
} 
mListView.setAdapter(adapter); 

但是,這並不工作!它只給我Project 2頭和它下面的所有7個條目。任何人都可以告訴我什麼是錯的?我猜這跟setAdapter有關。謝謝!

回答

11

我不認爲你想要做什麼是可能的,你試圖做到這一點。當您使用addHeaderView時,它將ListAdapter包裝在HeaderViewListAdapter中。我看着它的文檔here,這似乎意味着你可以有多個標題,但他們都會在頂部(杜,頭)。

這聽起來像你真正想要的是分隔符......

你可以使用CommonWare's MergeAdapter。它將允許您插入適配器和視圖(以您希望的任何順序),並將它們全部作爲單個適配器呈現給列表視圖。您只需爲每個內容部分提交標題和適配器,然後將其設置爲您的列表。

僞代碼示例:

myMergeAdapter = new MergeAdapter(); 
myMergeAdapter.addView(HeaderView1); 
myMergeAdapter.addAdapter(listAdapter1); 
myMergeAdapter.addView(HeaderView2); 
myMergeAdapter.addAdapter(listAdapter2); 
setListAdapter(myMergeAdapter); 
2

我使用自定義Section Adapter這是最初由CommonsWare編碼實現多個頭的情況下,可以使listivew內部分,如書籍,遊戲等。看看下面的代碼。

科適配器:

package com.medplan.db; 

import java.util.ArrayList; 
import java.util.List; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Adapter; 
import android.widget.BaseAdapter; 




abstract public class SectionedAdapter extends BaseAdapter { 

    String TAG = "========SectionedAdapter============"; 

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(); 
    sections.clear(); 


} 

public void addSection(String caption, Adapter adapter) { 

    sections.add(new Section(caption, adapter)); 
} 


public void clear() { 

    sections.clear(); 
    notifyDataSetChanged(); 
} 


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

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

public long getItemId(int position) { 
    return(position); 
} 

class Section { 
    String caption = null; 
    Adapter adapter = null; 

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

內的活動使部分適配器對象,看看下面的代碼:

final SectionedAdapter adapter =new SectionedAdapter() 
       { 

         protected View getHeaderView(String caption, int index, View convertView,ViewGroup parent) 
         { 

         result=(TextView)convertView; 

         if (convertView==null) 
         { 
          result=(TextView)getLayoutInflater().inflate(R.layout.section_header,null); 

         } 

         result.setText(caption); 
         // temp=caption; 
         // ind=index; 

         return(result); 
         } 
        }; 

section_header.xml

<?xml version="1.0" encoding="utf-8"?> 

<TextView 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:background="@color/black" 
    android:textColor="#FFFFFF" 
    android:ellipsize="end" 
    android:textSize="11sp" 
    style="?android:attr/listSeparatorTextViewStyle" /> 


<!-- android:background="#515050"--> 

內的活動添加你想要像下面的部分儘可能多:

注:userPic & medPic是數組列表的名稱。

adapter.addSection("section first", new EfficientAdapter(getApplicationContext(),usersPic)); 


adapter.addSection("section second", new EfficientAdapter(getApplicationContext(),medPic)); 

listview.setAdapter(adapter); 
+0

我已經讀過它了。我想知道我是否可以不使用'MergeAdapter'。原來我不行。謝謝 – Harsh

0

我會用ExpandableListView解決這個問題。它不需要任何額外的庫。

  • 創建您的自定義適配器。
  • 提供您的數據並填寫您需要覆蓋的方法。
  • 與ExpandableListView設置適配器之後:
    • 覆蓋groupItemClick所以點擊組實際上不會展開視圖。
    • 循環遍歷適配器中的每個父代並將它們設置爲展開狀態。
相關問題