如何用两个不同的对象填充ExpandableListView

时间:2018-06-21 17:09:22

标签: android expandablelistview

我有一个可扩展列表视图,想用两个不同的对象填充它,当前我只是用Tareas对象填充子对象,但想用不同的对象(元)填充相同的子列表。我该怎么实现呢,因为我的适配器只是用Tareas对象填充列表,而没有使用Metas对象。

  

UPDATE

这是我的ExpandableListViewAdapter:

public class CalendarioAdapter extends BaseExpandableListAdapter {
private Context mContext;
private List<String> mCalendarioListHeader;
private HashMap<String, List<Object>> mCalendarioListChild;
private static final int CHILD_TYPE_META = 0;
private static final int CHILD_TYPE_PLANES = 1;


public CalendarioAdapter(Context context, List<String> listDataHeader,
                         HashMap<String, List<Object>> listChildData) {
    this.mContext= context;
    this.mCalendarioListHeader= listDataHeader;
    this.mCalendarioListChild = listChildData;
}
@Override
public int getChildTypeCount() {
    return 2;
}


@Override
public int getGroupCount() {
    return this.mCalendarioListHeader.size();
}

@Override
public int getChildrenCount(int groupPosition) {
    List childList = mCalendarioListChild.get(mCalendarioListHeader.get(groupPosition));
    if (childList != null && ! childList.isEmpty()) {
        return childList.size();
    }
    return 0;
}


@Override
public Object getGroup(int groupPosition) {
    return this.mCalendarioListHeader.get(groupPosition);
}

@Override
public Object getChild(int groupPosition, int childPosition) {
    return this.mCalendarioListChild.get(this.mCalendarioListHeader.get(groupPosition))
            .get(childPosition);
}

@Override
public long getGroupId(int groupPosition) {
    return groupPosition;
}

@Override
public long getChildId(int groupPosition, int childPosition) {
    return childPosition;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
    String headerTitle = (String) getGroup(groupPosition);
    String listSize = getChildrenCount(groupPosition) +"";
    if (convertView == null) {
        LayoutInflater infalInflater = (LayoutInflater) this.mContext
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = infalInflater.inflate(R.layout.calendario_list_header, null);
    }

    TextView lblListHeader = (TextView) convertView
            .findViewById(R.id.lblListHeader);
    TextView sizeOfList = (TextView) convertView.findViewById(R.id.calendarioListSize);
    sizeOfList.setText(listSize);
    lblListHeader.setTypeface(null, Typeface.BOLD);
    lblListHeader.setText(headerTitle);

    return convertView;
}

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


    Integer childType = (getChildType(groupPosition, childPosition));
    Log.d("childType", childType+"");

    LayoutInflater layoutInflater = (LayoutInflater) this.mContext
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    if (convertView == null|| convertView.getTag() != childType) {

        switch (childType) {

            case CHILD_TYPE_META:
                convertView = layoutInflater.inflate(R.layout.metas_list_row, null);
                convertView.setTag(childType);
                break;
            case CHILD_TYPE_PLANES:
                convertView =  layoutInflater.inflate(R.layout.calendario_list_row, null);
                convertView.setTag(childType);
                break;
            default:

                break;
        }
    }
    switch (childType) {
        case CHILD_TYPE_META:
            Metas metas = (Metas) getChild(groupPosition, childPosition);
            TextView txtMetasChild =  convertView.findViewById(R.id.metasTxtChild);
            TextView fechaMetasChild = convertView.findViewById(R.id.fechaMetaChild);
            txtMetasChild.setText(metas.getMeta());
            fechaMetasChild.setText(metas.getFecha());
            break;
        case CHILD_TYPE_PLANES:
            Tareas tareas = (Tareas) getChild(groupPosition, childPosition);
            TextView txtDescripcionChild = (TextView) convertView
                    .findViewById(R.id.descripcionTareaText);
            TextView txtListaChild = (TextView) convertView
                    .findViewById(R.id.listaTareaText);
            txtDescripcionChild.setText(tareas.getDescripcion());
            txtListaChild.setText(tareas.getLista());
            break;

        default:
            break;
    }

    return convertView;


  }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    @Override
    public int getChildType(int groupPosition, int childPosition) {
        Object child = getChild(groupPosition, childPosition);
        Log.d("ChildType", child+"");
        if(child instanceof Metas){
            return CHILD_TYPE_META;
        } else if(child instanceof Tareas) {
            return CHILD_TYPE_PLANES;
        }
        else{
// should never happen, so do error handling here
            throw new RuntimeException("only classes Metas and Tareas permitted");
        }
    }
}
  

在我的片段上,我像这样填充列表:

       private void prepareListData() {
        listDataHeader = new ArrayList<String>();
        listDataChild = new HashMap<String, List<Object>>();

        // Adding header data
        listDataHeader.add(diasText[0]);
        listDataHeader.add(diasText[1]);
        listDataHeader.add(diasText[2]);
        listDataHeader.add(diasText[3]);
        listDataHeader.add(diasText[4]);
        listDataHeader.add(diasText[5]);
        listDataHeader.add(diasText[6]);

        // Adding child data

        listDataChild.put(listDataHeader.get(0), dia1List);
        listDataChild.put(listDataHeader.get(1), dia2List);
        listDataChild.put(listDataHeader.get(2), dia3List);
        listDataChild.put(listDataHeader.get(3), dia4List);
        listDataChild.put(listDataHeader.get(4), dia5List);
        listDataChild.put(listDataHeader.get(5), dia6List);
        listDataChild.put(listDataHeader.get(6), dia7List);
    }

任何关于如何实现我的要求的想法将不胜感激!

1 个答案:

答案 0 :(得分:1)

BaseExpandableListAdapter进行子类化时,可以为组项目以及子项目提供不同的View类型。我只会概述如何处理不同的子View类型,因为这就是您想要做的(即使这样,帖子也会足够长)。

View类型由int值表示,例如

private static final int TYPE_TAREAS = 0;
private static final int TYPE_METAS = 2;

因此,在您的情况下,您传入HashMap<String, List<Object>>而不是HashMap<String, List<Tareas>>并覆盖getChildTypeCount()以返回2(不同Viewtype的数量)。 (当然,您也可以使用TareasMetas的任何共同祖先代替Object

然后,您覆盖getChildType (int groupPosition, int childPosition),以为该特定条目的int类型返回正确的View值(TYPE_TAREAS或TYPE_METAS)。要确定类型,请在instanceof条目上使用List

最后,当覆盖getChildView()时,您需要对List条目进行强制类型转换,并通过调用View确定getChildType()类型。与ListView一样,您可以重新使用 convertView

documentation for getChildView()关于这个主题的内容是这样的:

  

convertView视图:如有可能,可重用的旧视图。使用前,应检查此视图是否为非null并具有适当的类型。如果无法转换此视图以显示正确的数据,则此方法可以创建一个新视图。不保证convertView以前已经由getChildView(int,int,boolean,View,ViewGroup)创建。

如果 convertView 无法重用,则通过填充所需的布局,根据View类型创建一个新的View。 遵循ViewHolder模式是一个好主意。您可能需要两个完全不同的ViewHolders或一个从另一个扩展的ViewHolders,这取决于两个数据类之间的差异。

其余的一切照旧:-)