Android 如何在可展开列表视图中插入标题行
我有一个Android活动,有一个可扩展的列表视图。然后我创建了我的ExpandableListAdapter,这样我就可以单击一个项目,并且可以看到这个项目的子项。这没关系。 现在,我想为每个项目的子项列表插入一个标准标题(Title)。因此,我构建了以下代码:Android 如何在可展开列表视图中插入标题行,android,expandablelistview,Android,Expandablelistview,我有一个Android活动,有一个可扩展的列表视图。然后我创建了我的ExpandableListAdapter,这样我就可以单击一个项目,并且可以看到这个项目的子项。这没关系。 现在,我想为每个项目的子项列表插入一个标准标题(Title)。因此,我构建了以下代码: public class ResultExpandableListAdapter extends BaseExpandableListAdapter { private Context context; privat
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;
}
}
公共类ResultExpandableListAdapter扩展了BaseExpandableListAdapter{
私人语境;
私有列表列表列表;
私有HashMap expandableListDetail;
public ResultExpandableListAdapter(上下文上下文、列表列表结果、,
HashMap expandableListDetail){
this.context=上下文;
this.listaRisultati=listaRisultati;
this.expandableListDetail=expandableListDetail;
}
@凌驾
公共对象getChild(int listPosition,int expandedListPosition){
返回this.expandableListDetail.get(this.listaRisultati.get(listPosition.getId())
.get(expandedListPosition);
}
@凌驾
公共长getChildId(int-listPosition、int-expandedListPosition){
返回expandedListPosition;
}
@凌驾
公共视图getChildView(int listPosition,最终int expandedListPosition,
布尔值isLastChild、视图转换视图、视图组父级){
if(convertView==null){
LayoutInflater LayoutInflater=(LayoutInflater)this.context
.getSystemService(上下文布局\充气机\服务);
if(expandedListPosition==0)
{
convertView=LayoutFlater.inflate(R.layout.results\u x\u result\u list\u item\u header,空);
}
如果(expandedListPosition>0&&expandedListPosition您是对的。为了向expandableListview添加页眉和页脚,您可以进行一些修改
getChildrenCount()
方法返回特定组中子项的行数。在这里,我们可以向返回方法添加两行额外的行。第一行将用于页眉,第二行将用于页脚。我们可以将特定布局膨胀到第一行(页眉)和最后一行(页脚)组的标题。您可以使用以下代码轻松地将标题添加到expandablelistview:
View header=getLayoutInflater().inflate(R.layout.nav_header, null);
mExpandableListView.addHeaderView(header);
只需在您的活动中复制上述代码。在上述代码中(R.layout.nav_标题)是header的xml文件,而mExpandableListView是ExpandableListView的对象。您在这个问题上有点困惑。请清楚地说明您在ExpandableList视图中所指的header行。好的。得到了您想要做的。您正在以正确的方式进行操作。:)将下面的解决方案标记为正确。`这是简单页眉和页脚的正确解决方案.addFooterView(headerView);