2015-10-12 65 views

回答

0

您可以使用ExpandableListView您的數據源是一個ArrayMap<String, ArrayList<Object>>

這裏是你如何管理你的意見對你的不同型號不完整的例子。

@Override 
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { 

     switch (groupPosition) { 
      case 1 : 
       //TODO: if the layout is different for each group you should check if the convertedView is the one you should be using for this group. 
       //TODO: if it isn't you should inflate, if it is you should reuse 
       if (convertView == null || !convertView.getTag() instanceof KittyViewHolder.class) { 
        convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.adapter_kitties, null); 
        holder = new KittyViewHolder(); 
        holder.name = (TextView) view.findViewById(R.id.kittyname); 
        convertView.setTag(holder); 
       } 
       else { 
        holder = (KittyViewHolder) view.getTag(); 
       } 

       //You can savely cast since you know your first group contains Kitties (=^・^=) 
       Kitty aKitty = (Kitty) getChild(groupPosition, childPosition); 
       holder.name.setText(aKitty.getName()); 
       break; 
      case 2: 
       //TODO: if the layout is different for each group you should check if the convertedView is the one you should be using for this group. 
       //TODO: if it isn't you should inflate, if it is you should reuse 
       if (convertView == null || !convertView.getTag() instanceof SharkHolder.class) { 
        convertView = ((Activity) mContext).getLayoutInflater().inflate(R.layout.adapter_sharks, null); 
        holder = new SharkHolder(); 
        holder.thooth = (TextView) view.findViewById(R.id.thoothcount); 
        holder.picture = (ImageView) view.findViewById(R.id.sharkimage); 
        convertView.setTag(holder); 
       } 
       else { 
        holder = (SharkHolder) view.getTag(); 
       } 

       //You can savely cast since you know your second group contains Sharks 
       Shark aShark = (Shark) getChild(groupPosition, childPosition); 
       holder.thooth.setText(""+aShark.getToothCount()); 
       holder.picture.setImageResource(aShark.getBabySharkPicture()); 
       break; 
      case 3: 
       //Same thing as above with the correct model type 
       break; 
     } 

     return convertView; 
    } 
+0

我正在使用可擴展列表視圖..因爲我需要爲同一個listview設置不同的適配器。這可能嗎?因爲所有3個模型類都是不同的。 – Dilipchandar

+0

我不明白爲什麼不。如果你的模型類共享一個通信父或接口,你的ArrayList將是ArrayList ,否則它將是ArrayList 。你需要檢查'getGroupView'和'getChildView'你正在處理的對象。 – Kalem

+0

如果我將相同的listview設置爲兩個適配器,它只顯示一組適配器。 – Dilipchandar

0

您應該學習如何使用部分。

您可以閱讀SO上的answer。或直接按照這些tutorial(也出現在原始答案中)。

在你的情況,你應該有一個適配器與三個不同的數據陣列。我們稱之爲array1,array2array3

您應該具有以下getCount方法:

public int getCount() { 
    return array1.length + array2.length + array3.length + 3;//3 for 3 header 
} 

那麼你應該有4項視圖類型(每個陣列和一個用於頭)

public int getItemViewTypeCount() { 
    return 4 
} 

那麼你應該告訴你適配器至於每個位置的視圖類型

public int getItemViewType(int position) { 
    if (position == 0) { 
      return VIEW_TYPE_HEADER; 
    } else if (position < 1 + array1.length) { 
      return VIEW_TYPE_1; 
    } else if (position == 1 + array1.length) { 
      return VIEW_TYPE_HADER; 
    } else if (position < 2 + array1.length + array2.length) { 
      return VIEW_TYPE_2; 
    } else if (position == 2 + array1.length + array2.length) { 
      return VIEW_TYPE_HADER; 
    } else if (position < 3 + array1.length + array2.length + array3.length) { 
      return VIEW_TYPE_3; 
    } else { 
      return 0;//this should never happened 
    } 
} 

現在你可以imp字元素getView

public View getView(int position, View convertView, ViewGroup parent) { 
     switch (getItemViewType(position)) { 
      case VIEW_TYPE_HEADER: 
        getHeaderView(positin, convertView, parent); 
        break; 
      case VIEW_TYPE_1: 
        getViewType1(positin - 1, convertView, parent); 
        break; 
      case VIEW_TYPE_2: 
        getViewType1(positin - 2 - array1.length, convertView, parent); 
        break; 
      case VIEW_TYPE_2: 
        getViewType1(positin - 3 - array1.length - array2.length, convertView, parent); 
        break; 
      default: 
        return null; 
     } 
} 

然後,您應該實現getViewTypeX(int position, View convertView, ViewGoup parent)應該建立視圖對象arrayX[position],像往常一樣。

+0

列表視圖這看起來簡單的字符串數組。但是,當涉及到自定義模型,發現很難實現。我可以爲同一個listview設置三個不同的適配器,因爲我有三個自定義數組列表? – Dilipchandar

+0

我編輯我的答案,使其更容易理解(我希望)。 – sonic

+0

謝謝sonic – Dilipchandar