我已经实施了ExpandableListView
和BaseExpandableListAdapter
。我想显示一个GroupObjects列表。每个GroupObject(ExpandableList中的顶级项)都有一个3个ChildObjects的ArrayList(ExpandableList中的子项)。
我正在使用viewHolder-pattern。
GroupObjects正确显示。展开第一个GroupObject后,getChildView()
被调用3次并正确显示其ChildObjects。
但是,只要我展开另一个GroupObject,getChildView()
就会返回第一个GroupObject 的ChildObjects。由于第二次调用的convertView不为null,因此它总是返回相同的ChildObjects。
以下是一些代码:
@Override
public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
final ChildObject childobject = (ChildObject)getChild(groupPosition, childPosition);
ChildViewholder childViewholder;
if (convertView == null) {
childViewholder = new ChildViewholder();
LayoutInflater inflater = (LayoutInflater)context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (!childobject.isReserved()) {
convertView = inflater.inflate(R.layout.childobject_free,
parent, false);
childViewHolder.layout = (RelativeLayout)convertView
.findViewById(R.id.childobject_free_layout);
} else {
final Reservation reservation = childobject.getReservation();
convertView = inflater.inflate(
R.layout.childobject_reserved, parent, false);
childViewHolder.layout = (RelativeLayout)convertView.findViewById(R.id.reserved_layout);
childViewHolder.reservationImage = (ImageView)convertView.findViewById(R.id.reservationImage);
childViewHolder.reservationName = (TextView)convertView.findViewById(R.id.reservationName);
childViewHolder.buttonFinish = (ImageButton)convertView
.findViewById(R.id.finishButton);
childViewHolder.buttonCancel = (ImageButton)convertView
.findViewById(R.id.cancelButton);
childViewHolder.progressBar = (ProgressBar)convertView
.findViewById(R.id.reservationProgress);
childObject.getTimer().setProgressBar(childViewHolder.progressBar);
childViewHolder.reservationName.setText(reservation.getName());
childViewHolder.reservationImage.setImageBitmap(reservation.getBitmap());
childViewHolder.buttonFinish.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
DrawerClickHandler.finishReservation(reservation);
}
});
childViewHolder.buttonCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DrawerClickHandler.cancelReservation(reservation);
}
});
}
convertView.setTag(childViewholder);
}else{
childViewholder = (ChildViewholder)convertView.getTag();
}
childViewHolder.groupPosition = groupPosition;
childViewHolder.childPosition = childPosition;
return convertView;
}
在getChildView()
的前3次调用中,convertView仅为null。
如果您需要更多代码,请与我们联系。
感谢任何帮助。