我有3個數組列表,它從各種模型類獲取數據..我想在單個列表視圖中顯示這3個單獨的標題。我怎樣才能做到這一點?誰能幫我?填充單個列表視圖與多個自定義數組列表
回答
您可以使用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;
}
我正在使用可擴展列表視圖..因爲我需要爲同一個listview設置不同的適配器。這可能嗎?因爲所有3個模型類都是不同的。 – Dilipchandar
我不明白爲什麼不。如果你的模型類共享一個通信父或接口,你的ArrayList將是ArrayList
如果我將相同的listview設置爲兩個適配器,它只顯示一組適配器。 – Dilipchandar
您應該學習如何使用部分。
您可以閱讀SO上的answer。或直接按照這些tutorial(也出現在原始答案中)。
在你的情況,你應該有一個適配器與三個不同的數據陣列。我們稱之爲array1
,array2
和array3
。
您應該具有以下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]
,像往常一樣。
列表視圖這看起來簡單的字符串數組。但是,當涉及到自定義模型,發現很難實現。我可以爲同一個listview設置三個不同的適配器,因爲我有三個自定義數組列表? – Dilipchandar
我編輯我的答案,使其更容易理解(我希望)。 – sonic
謝謝sonic – Dilipchandar
- 1. 多行列表視圖填充數組?
- 2. 填充自定義列表視圖與來自多個編輯文本
- 3. 填充列表視圖與一個ArrayList
- 4. 當我點擊另一個自定義列表視圖項目時填充自定義列表視圖
- 5. 填充列表視圖與自定義佈局項目
- 6. 如何用兩個數組填充自定義列表。
- 7. 從多個表填充列表視圖SQLite數據庫
- 8. 通過自定義BaseAdapter傳遞多個ArrayList來填充列表視圖
- 9. 如何填充與多個Firebase項目的列表視圖
- 10. 自定義列表視圖自定義列表視圖Android
- 11. 填充有兩個textviews自定義列表視圖和一個複選框
- 12. 列表視圖與多個列表
- 13. 如何用三個數組填充列表視圖?
- 14. 與圖像自定義列表視圖
- 15. 使用ListFragment填充自定義視圖列表
- 16. 自定義Simplecursoradapter不會填充列表視圖
- 17. 自定義列表視圖沒有得到填充!
- 18. android自定義列表視圖與自定義單選按鈕
- 19. 填充列表視圖
- 20. 填充列表視圖
- 21. 從組合框填充列表視圖
- 22. 從多個數據庫行填充列表視圖行
- 23. 填充下拉列表選擇與數組 - 有多個選項
- 24. 將數組列表添加到自定義列表視圖
- 25. 什麼填充這個數組列表?
- 26. 無法填充自定義列表視圖行時組件列表視圖上面
- 27. 如何根據2個布爾表列填充列表視圖?
- 28. 自動填充表列與另一個表的列?
- 29. 自定義列表視圖
- 30. 自定義列表視圖
指在android系統 – Rustam