Android 对ExpandableListView中的子项使用两种不同的布局
我正在尝试使用Android 对ExpandableListView中的子项使用两种不同的布局,android,expandablelistview,Android,Expandablelistview,我正在尝试使用ExpandableListView根据项目类型使用两种不同的布局。此时,我制作了基本功能,除了一件事: 在列表视图中选择组后,子项的文本显示错误(不同项的文本混合) 我的适配器源代码如下: SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter( this, groupData, com.oleg.mart.foreign.R
ExpandableListView
根据项目类型使用两种不同的布局。此时,我制作了基本功能,除了一件事:
在列表视图中选择组后,子项的文本显示错误(不同项的文本混合)
我的适配器
源代码如下:
SimpleExpandableListAdapter adapter = new SimpleExpandableListAdapter(
this,
groupData,
com.oleg.mart.foreign.R.layout.group_row,
groupFrom,
new int[] { com.oleg.mart.foreign.R.id.Group },
childData,
R.layout.simple_list_item_1,
childFrom,
childTo)
{
@Override
public int getChildTypeCount() {
return 2;
}
/*@Override
public int getChildrenCount(int groupPosition)
{
return childData.get(groupPosition).size();
} */
@Override
public int getChildType(int groupPosition, int childPosition)
{
int result = 0;
if (childPosition == getChildrenCount(groupPosition) - 1)
result = 1;
return result;
}
@Override
public View getChildView (
int groupPosition, int childPosition,
boolean isLastChild,
View convertView, ViewGroup parent)
{
if (convertView == null) {
TextView textView = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
int itemType = getChildType(groupPosition,childPosition);
String myText = childData.get(groupPosition).get(childPosition).get("chapter");
switch (itemType) {
case 0:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast, null);
textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));
break;
case 1:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row, null);
textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.Child);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));
break;
}
}
return convertView;
}
};
正如您所看到的,我试图在最后一个子项上设置不同的样式。有什么不对劲吗
右getChildView
方法:
TextView textView = null;
String myText = null;
myText = childData.get(groupPosition).get(childPosition).get("chapter");
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
int itemType = getChildType(groupPosition,childPosition);
switch (itemType) {
case 0:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast, null);
break;
case 1:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row, null);
break;
}
}
textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));
return convertView;
我敢肯定,在convertView!=getChildView方法上为null。
尝试在条件之后设置值,如下所示:
TextView textView = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater)getSystemService(getBaseContext().LAYOUT_INFLATER_SERVICE);
int itemType = getChildType(groupPosition,childPosition);
String myText = childData.get(groupPosition).get(childPosition).get("chapter");
switch (itemType) {
case 0:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row_notlast, null);
break;
case 1:
convertView = inflater.inflate(com.oleg.mart.foreign.R.layout.child_row, null);
break;
}
}
textView = (TextView)convertView.findViewById(com.oleg.mart.foreign.R.id.NChild);
textView.setPadding(30,20,30,20);
textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 15);
textView.setText(Html.fromHtml(myText));
如果您对此仍有疑问,请对我的答案发表评论,告诉我,我会看看我能做些什么。使用HeterogeneousExpandableList。
()
它提供了在可扩展列表视图中具有不同标题/子视图的功能,并保持了其重用性。非常感谢您快速、高质量的回答!这不是造成了问题吗?如果convertView不为null怎么办?我们如何知道它是哪个布局(假设不同布局中有不同的ID),这取决于您拥有什么,但实际上,如果您使用不同的内容管理不同的项目视图类型,则必须检查转换视图的状态。一种常见的方法是在convertView上使用“setTag”和“getTag”。在我的回答中,我希望在布局com.oleg.mart.foreign.R.id.NChild和com.oleg.mart.foreign.R.layout.child\u row\u notlast和com.oleg.mart.foreign.R.layout.child\u row上都有id“com.oleg.foreign.R.id.NChild”。@Sebastien在使用ViewHolder的情况下,该解决方案将如何工作。?