android - 如何在可扩展 ListView 中插入标题行

标签 android expandablelistview

我有一个带有一个可扩展 ListView 的 Android Activity 。然后我创建了我的 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 列表(例如),我只能读取第二个项目。

这个问题有解决办法,或者有其他方法可以在 subview 中插入标题?

这是我正在运行的应用程序: enter image description here

我的标题行是黄色行“Parameter, Value...”

最佳答案

您可以使用以下代码轻松地将标题添加到可扩展 ListView :

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

只需将上述代码复制到您的 Activity 中即可。上面代码中(R.layout.nav_header)是header的xml文件,mExpandableListView是ExpandableListView的对象。

关于android - 如何在可扩展 ListView 中插入标题行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37046055/

相关文章:

Android - 没有调试日志的 adb logcat

android - 问题: Why react native video does not play video in full screen?

java - 我的 Activity 没有调用 onActivityResult

android - 有人可以指出我在下面的程序中缺少什么吗

Android Expandable ListView - 如何平滑滚动到组内的子位置?

android - 为 ExpandableListView 设置 onChildClickListener 时出现问题

json - 如何在 flutter 中使用 Json 添加带有复选框列表图 block 的多个展开图 block

android - ExpandableListView 中的按钮 - 不寻常的标签行为

android - 在我的案例中如何在可扩展 ListView 中获取 View 不显示它显示空白的可扩展列表

android - 使用 espresso 在导航菜单中向下滚动