在我的 ExpandableListView 中,我希望每个组的第一个子项与组中的其他子项具有不同的 xml 视图。
我想要实现的是这样的:
我设法用以下代码做到这一点:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View newView, ViewGroup parent) {
if (newView==null){
// CHILD HEADER
if (childPosition == 0){
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newView = infalInflater.inflate(R.layout.super_trener_cell_child_header, null);
// CHILD CELL
else{
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
newView = infalInflater.inflate(R.layout.super_trener_cell_child, null);
}
}
return newView;
}
这很好用。然而,这是一个问题..每当我扩大和缩小单元格时,我都会得到另一个子单元格的出现顺序。
以下是一些屏幕截图,显示了正在发生的事情(数字代表它们应该出现的顺序):
每次单击组标题后,此单元格反转的原因可能是什么?
编辑:整个适配器代码:
公共类 SuperTrenerCellAdapter 扩展 BaseExpandableListAdapter {
Context context;
List<SuperTrenerTopic> topics = new LinkedList<SuperTrenerTopic>();
private static LayoutInflater inflater = null;
public SuperTrenerCellAdapter(List<SuperTrenerTopic> topics, Context context) {
this.topics = topics;
this.context = context;
}
@Override
public int getGroupCount() {
return topics.size();
}
@Override
public int getChildrenCount(int groupPosition) {
SuperTrenerTopic topic = topics.get(groupPosition);
return topic.getLessonList().size()+1;
}
@Override
public Object getGroup(int groupPosition) {
return topics.get(groupPosition);
}
@Override
public Object getChild(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.super_trener_cell_header, null);
}
SuperTrenerTopic topic = topics.get(groupPosition);
TextView topicTitle = (TextView)convertView.findViewById(R.id.stc_topicName);
topicTitle.setText(topic.getName());
return convertView;
}
@Override
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
SuperTrenerTopic topic = topics.get(groupPosition);
if (convertView==null){
// CHILD HEADER
if (childPosition == 0){
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.super_trener_cell_child_header, null);
}
// LESSON CELL
else{
LayoutInflater infalInflater = (LayoutInflater) this.context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.super_trener_cell_child, null);
SuperTrenerLesson stl = topic.getLessonList().get(childPosition-1);
TextView title = (TextView)convertView.findViewById(R.id.st_cell_child_title);
title.setText(""+stl.getTitle());
}
}
return convertView;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
if (childPosition == 0){
return false;
}
else{
return true;
}
}
}