使用多个自定义数组列表填充单个列表视图

时间:2015-10-12 11:56:50

标签: android

我有3个数组列表,它从各种模型类中获取数据。我想在单个列表视图中显示这3个带有单独的标题。我怎样才能做到这一点?谁能帮我?

2 个答案:

答案 0 :(得分:0)

您应该学习如何使用部分。

您可以在SO上阅读此answer。或者直接关注这些tutorial(也会出现在原始答案中)。

在您的情况下,您应该有一个具有三个不同数据阵列的适配器。我们称他们为array1array2array3

您应该使用以下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;
       }
 }

然后您应该像往常一样实现应该为对象getViewTypeX(int position, View convertView, ViewGoup parent)构建视图的arrayX[position]

答案 1 :(得分:0)

您可以使用数据来源为ArrayMap<String, ArrayList<Object>>

ExpandableListView

以下是一个不完整的示例,说明如何管理不同模型的观看次数。

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