我有三种组类型,TYPE_SEPARATOR、TYPE_EXAM、TYPE_HOMEWORK,后两种我需要展开 onClick 工作正常。我不想夸大 TYPE_SEPARATOR 的视图,但我正在努力找出如何实现这一点。任何回应将不胜感激。此适配器正在使用 BaseExpandableListAdapter。
这是给 childViews
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater vi;
int type = getGroupType(groupPosition);
days = 0;
switch (type){
case TYPE_SEPARATOR:
v = null;
break;
case TYPE_EXAM:
Exam exam = (Exam) objects.get(groupPosition);
if (v == null) {
vi = context.getLayoutInflater();
v = vi.inflate(R.layout.expandeditem, null);
holder.tV_expandedItem_days = (TextView) v.findViewById(R.id.tV_expandedItem_days);
holder.tV_expandedItem_grade = (TextView) v.findViewById(R.id.tV_expandedItem_grade);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
formatter = DateTimeFormat.forPattern("dd MMM YYYY");
now = new DateTime();
objectDate = DateTime.parse(exam.getDate(), formatter);
d = Days.daysBetween(now, objectDate);
days = d.getDays() + 1;
if (days > 0){
holder.tV_expandedItem_days.setText(String.valueOf(days) + " days to go");
}
else {
days = days * -1;
holder.tV_expandedItem_days.setText(String.valueOf(days) + " days ago");
}
holder.mark = exam.getMark();
holder.maxMark = exam.getMaxMark();
if (exam.getMaxMark() > 0) {
holder.tV_expandedItem_grade.setVisibility(View.VISIBLE);
holder.percentage = (holder.mark / holder.maxMark) * 100;
holder.tV_expandedItem_grade.setText(new Grade().get(holder.percentage));
} else {
holder.tV_expandedItem_grade.setText("?");
}
break;
case TYPE_HOMEWORK:
Homework homework = (Homework) objects.get(groupPosition);
if (v == null) {
vi = context.getLayoutInflater();
v = vi.inflate(R.layout.expandeditem, null);
holder.tV_expandedItem_days = (TextView) v.findViewById(R.id.tV_expandedItem_days);
holder.tV_expandedItem_grade = (TextView) v.findViewById(R.id.tV_expandedItem_grade);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
formatter = DateTimeFormat.forPattern("dd MMM YYYY");
now = new DateTime();
objectDate = DateTime.parse(homework.getDate(), formatter);
d = Days.daysBetween(now, objectDate);
days = d.getDays() + 1;
if (days > 0){
holder.tV_expandedItem_days.setText(String.valueOf(days) + " days to go");
}
else {
days = days * -1;
holder.tV_expandedItem_days.setText(String.valueOf(days) + " days ago");
}
holder.mark = homework.getMark();
holder.maxMark = homework.getMaxMark();
if (homework.getMaxMark() > 0) {
holder.percentage = (holder.mark / holder.maxMark) * 100;
holder.tV_expandedItem_grade.setText(new Grade().get(holder.percentage));
} else {
holder.tV_expandedItem_grade.setText("?");
}
break;
}
return v;
}
这是针对 groupViews
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
View v = convertView;
int type = getGroupType(groupPosition);
holder = new ViewHolder();
LayoutInflater vi;
switch (type) {
case TYPE_SEPARATOR:
if (v == null) {
vi = context.getLayoutInflater();
v = vi.inflate(R.layout.completedseparator, null);
} else {
holder = (ViewHolder) v.getTag();
}
break;
case TYPE_EXAM:
if (v == null) {
vi = context.getLayoutInflater();
v = vi.inflate(R.layout.examitem, null);
holder.tV_title = (TextView) v.findViewById(R.id.tV_title);
holder.tV_startTime = (TextView) v.findViewById(R.id.tV_startTime);
holder.tV_date = (TextView) v.findViewById(R.id.tV_date);
holder.tV_grade = (TextView) v.findViewById(R.id.tV_grade);
holder.tV_grade.setVisibility(View.GONE);
holder.tV_title.setEllipsize(TruncateAt.MARQUEE);
holder.tV_title.setSelected(true);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.exam = (Exam) objects.get(groupPosition);
if ((holder.exam != null)) {
holder.tV_title.setText(holder.exam.getTitle());
holder.tV_startTime.setText(holder.exam.getStartTime());
holder.tV_date.setText(holder.exam.getDate());
holder.tV_title.setText(holder.exam.getTitle());
holder.tV_startTime.setText(holder.exam.getStartTime());
holder.tV_date.setText(holder.exam.getDate());
}
break;
case TYPE_HOMEWORK:
if (v == null) {
vi = context.getLayoutInflater();
v = vi.inflate(R.layout.homeworkitem, null);
holder.tV_homework_subject = (TextView) v
.findViewById(R.id.tV_homework_subject);
holder.tV_homework_description = (TextView) v
.findViewById(R.id.tV_homework_description);
holder.tV_homework_date = (TextView) v
.findViewById(R.id.tV_homework_date);
holder.tV_homework_grade = (TextView) v
.findViewById(R.id.tV_homework_grade);
holder.tV_homework_time = (TextView) v
.findViewById(R.id.tV_homework_time);
holder.tV_homework_grade.setVisibility(View.GONE);
holder.tV_homework_description.setEllipsize(TruncateAt.MARQUEE);
holder.tV_homework_description.setSelected(true);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
holder.homework = (Homework) objects.get(groupPosition);
holder.tV_homework_subject.setText(holder.homework.getSubject());
holder.tV_homework_description.setText(holder.homework.getDescription());
holder.tV_homework_date.setText(holder.homework.getDate());
holder.tV_homework_time.setText(holder.homework.getTime());
break;
}
return v;
}
编辑!
我的适配器的问题是 getChildrenCount 方法总是返回 1。这是新方法。
@Override
public int getChildrenCount(int groupPosition) {
int count = 0;
if (objects.get(groupPosition).getClass().equals(String.class)) {
count = 0;
}
if (objects.get(groupPosition).getClass().equals(Exam.class)) {
count = 1;
}
if (objects.get(groupPosition).getClass().equals(Homework.class)) {
count = 1;
}
return count;
}
感谢提琴手帮助我!