如何在可扩展列表视图中插入标题行

3

我有一个带有可展开列表视图的Android活动。然后我创建了我的ExpandableListAdapter,这样我就可以单击一个项,看到此项的子项,这很好。 现在我想为每个项的子项插入标准标题。因此,我已经构建了以下代码:

public class ResultExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<Result> listaRisultati;
    private HashMap<Integer, List<ResultXResult>> expandableListDetail;

    public ResultExpandableListAdapter(Context context, List<Result> listaRisultati,
                                       HashMap<Integer, List<ResultXResult>> expandableListDetail) {
        this.context = context;
        this.listaRisultati = listaRisultati;
        this.expandableListDetail = expandableListDetail;
    }

    @Override
    public Object getChild(int listPosition, int expandedListPosition) {
        return this.expandableListDetail.get(this.listaRisultati.get(listPosition).getId())
                .get(expandedListPosition);
    }

    @Override
    public long getChildId(int listPosition, int expandedListPosition) {
        return expandedListPosition;
    }

    @Override
    public View getChildView(int listPosition, final int expandedListPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            if(expandedListPosition ==0)
            {
                convertView = layoutInflater.inflate(R.layout.results_x_result_list_item_header, null);
            }

            if(expandedListPosition>0 && expandedListPosition<=getChildrenCount(expandedListPosition))
            {
                ResultXResult risultato = (ResultXResult) getChild(listPosition, expandedListPosition);
                convertView = layoutInflater.inflate(R.layout.results_x_result_list_item,null);

                TextView parameter = (TextView) convertView
                        .findViewById(R.id.parameter);

                TextView valueUM = (TextView) convertView
                        .findViewById(R.id.valueUm);

                TextView interpretation = (TextView) convertView
                        .findViewById(R.id.interpretation);

                TextView um = (TextView) convertView
                        .findViewById(R.id.um);


                parameter.setText(risultato.getInfo().getDisplayName());
                valueUM.setText(risultato.getValue());
                interpretation.setText(risultato.getInterpretationCode().getDisplayName());
                um.setText(risultato.getUnitaDiMisura());
            }
        }

        return convertView;
    }

    @Override
    public int getChildrenCount(int listPosition) {
        return this.expandableListDetail.get(this.listaRisultati.get(listPosition).getId())
                .size();
    }

    @Override
    public Object getGroup(int listPosition) {
        return this.listaRisultati.get(listPosition);
    }

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

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

    @Override
    public View getGroupView(int listPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        Result result = (Result) getGroup(listPosition);
        if (convertView == null) {
            LayoutInflater layoutInflater = (LayoutInflater) this.context.
                    getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = layoutInflater.inflate(R.layout.results_list_row, null);
        }
        TextView examination = (TextView) convertView
                .findViewById(R.id.examination);
        TextView startDate = (TextView) convertView
                .findViewById(R.id.startDate);
        TextView endDate = (TextView) convertView
                .findViewById(R.id.endDate);
        examination.setTypeface(null, Typeface.BOLD);

        examination.setText(result.getInfo().getDisplayName());
        startDate.setText(result.getDateStart());
        endDate.setText(result.getDateEnd()!=null ? result.getDateEnd()+" " : " ");
        return convertView;
    }

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

    @Override
    public boolean isChildSelectable(int listPosition, int expandedListPosition) {
        return true;
    }
}

我的临时解决方案是在“getChildView”方法中,如果是第一行,我可以插入一个布局,如果是另一行,则打印数据。因此,有了这段代码,如果我有一个具有2个结果项目的ResultXResult列表(例如),我只能读取第二个项目。
是否有解决此问题的解决方案,或者是否有另一种方法可以在子视图中插入标题?
这是我的应用程序运行情况:enter image description here 对于我来说,标题行是黄色行“参数,值...”。

你的问题表述不清楚,请明确说明你所指的可扩展列表视图(Expandable List View)中的标题行(header row)是指哪一部分。 - Janki Gadhiya
好的,我明白你想做什么。你正在正确的方式下进行,继续前进吧。 :) - Janki Gadhiya
请将下面的解决方案标记为正确。 - MobDev
2个回答

4

您可以使用以下代码轻松向可扩展列表视图添加标头:

View header=getLayoutInflater().inflate(R.layout.nav_header, null);
 mExpandableListView.addHeaderView(header);

只需将上述代码复制到您的活动中。在上面的代码中(R.layout.nav_header)是标头的xml文件,mExpandableListView是ExpandableListView对象。


`This is the right solution for simple header and footer .addFooterView(headerView); - MobDev

0

你说得对。为了给可扩展列表视图添加页眉和页脚,你可以进行一些修改。

getChildrenCount() 方法返回特定组中子项行的计数。在这里,我们可以向返回方法添加两行额外的行。第一行将用于页眉,第二行将用于页脚。我们可以将特定布局填充到组的第一行(页眉)和最后一行(页脚)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接