2014-01-19 20 views
0

我想在列表視圖中添加動態標頭,即列表視圖標頭的動態數據將在加載列表視圖時從解析的信息中獲得。在列表視圖中使用getItemType

請閱讀下面的我的方案。必須瞭解我的問題。

在這裏,我這樣做使用getItemType爲列表視圖的位置0。

@Override public View getView(final int position,View convertView,ViewGroup parent){ ViewHolder holder; if(convertView == null){

 int type = getItemViewType(position); 
     if(type==TYPE_HEADER){ 
      holder = new ViewHolder(); 
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.my_basket_small_item, parent,false); 
      convertView.setTag(holder); 
     }else{ 
      holder = new ViewHolder(); 
      LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
      convertView = inflater.inflate(R.layout.my_basket_small_item, parent,false); 

      holder.tv_price = (TextView)convertView.findViewById(R.id.tv_price); 
      holder.tv_distance = (TextView)convertView.findViewById(R.id.tv_distance); 
      holder.img_brandLogo = (ImageView)convertView.findViewById(R.id.img_brandlogo_top); 
      holder.img_beerCanLogo = (ImageView)convertView.findViewById(R.id.img_beerCan); 
      holder.click4map = (Button)convertView.findViewById(R.id.btn_click_for_map); 
      holder.offers = (Button)convertView.findViewById(R.id.btn_my_basket_offers); 
     } 
    } 
    else{ 
     holder = (ViewHolder) convertView.getTag(); 
    } 

return convertView;

我的問題是,我想顯示固定頭動態數據加列表視圖行(arraylist大小總數)。 現在它正在工作少一行,因爲一行被列表視圖標題佔據。

方案: 例如4列表視圖行未來(無頭) 當我使用頭,4再次行來了,我只是想,當我使用頭,1頭加4行應該來。

希望你明白。

感謝

回答

1

你應該通過調用添加標題:

<ListView>.addHeaderView(<HeaderView>,null, false); 

而且使用適配器只組項目。

在當前實現u能覆蓋:

@Override 
public int getCount() { 
    // TODO Auto-generated method stub 
    return <list>.size()+1; 
} 
+0

但頭部的數據應該是動態的,我必須改變它在適配器類 –

+0

發佈您的完整適配器。我沒有看到你設置了你發佈的任何數據。但重寫getCount應該適用於你的情況 –

1

還有一個辦法對這個問題 - 每個視圖項包含能見度頭在getView方法進行管理。 您的列表項佈局文件可能是這樣的:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

     <LinearLayout 
      android:id="@+id/header" 
      android:visibility="gone" 
      android:orientation="horizontal" 
      android:gravity="center_vertical" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content"> 
      <TextView 
       android:id="headerTextView" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"/> 
     </LinearLayout> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

      <ImageView 
       android:id="@+id/itemImageView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content"/> 

      <TextView 
       android:id="@+id/itemTextView" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" /> 
      </LinearLayout> 
     </LinearLayout> 
    </LinearLayout> 

和getView()可能是這樣的:

if(converView == null) { 
     LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     convertView = inflater.inflate(R.layout.my_basket_small_item, parent, false); 

     holder = new ViewHolder(); 
     holder.header = convertView.findViewById(R.id.header); 
     holder.headerTextView = (TextView)convertView.findViewById(R.id.headerTextView); 
     holder.tv_price = (TextView)convertView.findViewById(R.id.tv_price); 
     holder.tv_distance = (TextView)convertView.findViewById(R.id.tv_distance); 
     holder.img_brandLogo = (ImageView)convertView.findViewById(R.id.img_brandlogo_top); 
     holder.img_beerCanLogo = (ImageView)convertView.findViewById(R.id.img_beerCan); 
     holder.click4map = (Button)convertView.findViewById(R.id.btn_click_for_map); 
     holder.offers = (Button)convertView.findViewById(R.id.btn_my_basket_offers); 
    } else { 
     holder = (ViewHolder)convertView.getTag(); 
    } 

    int type = getItemViewType(position); 
    if(type==TYPE_HEADER){ 
     holder.header.setVisibility(View.SHOW); 
     holder.headerTextView.setText("Header"); 
    }else{ 
     holder.header.setVisibility(View.GONE); 
    } 

    ...