简体   繁体   English

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

[英]How to insert header row on Expandable List View

I have an Android activity with one expandable list view.我有一个带有一个可扩展列表视图的 Android 活动。 Then I have created my ExpandableListAdapter, so I can click on one items and I can see the child of this item.然后我创建了我的 ExpandableListAdapter,所以我可以点击一个项目,我可以看到这个项目的子项。 This is ok.还行吧。 Now I want to insert a standard header (Title) for a list of child to every items.现在我想为每个项目的子列表插入一个标准标题(标题)。 So I have build this code:所以我建立了这个代码:

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

The my temporary solution is in the method "getChildView", if is the first row, I can insert one layour, if is another row, I print the data.我的临时解决方案是在“getChildView”方法中,如果是第一行,我可以插入一个图层,如果是另一行,我打印数据。 So with this code, if I have a list of ResultXResult with 2 Items (for example), I can read only the second items.因此,使用此代码,如果我有一个包含 2 个项目的 ResultXResult 列表(例如),我只能读取第二个项目。

There is a solution of this problem, or there is another method to insert a header in child view?这个问题有解决方案,还是有其他方法在子视图中插入标题?

This is my app running:这是我的应用程序运行: 在此处输入图片说明

The header row for me is the yellow row "Parameter, Value...."我的标题行是黄色行“参数,值....”

You cam easily add header to expandablelistview by using the code below :您可以使用以下代码轻松地将标题添加到可扩展列表视图:

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

Just copy the above code in your activity.只需在您的活动中复制上述代码即可。 In the above code (R.layout.nav_header) is the xml file of header and mExpandableListView is the object of ExpandableListView.上面代码中(R.layout.nav_header)是header的xml文件,mExpandableListView是ExpandableListView的对象。

You are right.你是对的。 In order to add Header and footer to expandableListview, you can do some modifications.为了给expandableListview添加Header和footer,可以做一些修改。

getChildrenCount() method returns the count of row of child item in a specific group. getChildrenCount()方法返回特定组中子项的行数。 Here we can add two extra rows to the return method.在这里,我们可以向 return 方法添加两行额外的行。 The first row will be used for header and the second row will be used for footer.第一行将用于页眉,第二行将用于页脚。 and we can inflate the specific layout to first(header) and last row(footer) of a group.我们可以将特定布局膨胀到组的第一行(页眉)和最后一行(页脚)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM