在ExpandableListView中为子项使用两种不同的布局

我正在尝试执行ExpandableListView根据项目types使用两个不同的布局。 在这个时候,我做了一些基本的function,除了一件事情:在listviewselect一个组后,子项的文本显示错误(不同项目的文本混合)

我的adapter源代码如下:

 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; 

Solutions Collecting From Web of "在ExpandableListView中为子项使用两种不同的布局"

我非常肯定,当您的getChildView方法的convertView!= 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。 ( https://developer.android.com/reference/android/widget/HeterogeneousExpandableList.html )它提供了在可扩展列表视图中具有不同的头/子视图的function,并且还保持其重用。