【发布时间】:2015-10-12 11:56:50
【问题描述】:
我有 3 个数组列表,它们从各种模型类中获取数据。我想在一个带有单独标题的列表视图中显示这 3 个。我怎样才能做到这一点?谁能帮我?
【问题讨论】:
-
在android中引用listview
标签: android
我有 3 个数组列表,它们从各种模型类中获取数据。我想在一个带有单独标题的列表视图中显示这 3 个。我怎样才能做到这一点?谁能帮我?
【问题讨论】:
标签: android
您可以使用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;
}
【讨论】:
你应该学习如何使用部分。
您可以在 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
}
}
现在你可以实现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;
}
}
然后您应该像往常一样实现应该为对象arrayX[position] 构建视图的getViewTypeX(int position, View convertView, ViewGoup parent)。
【讨论】: