Когда записи впервые появляются на ListView
, они выглядят нормально, а getView
if (convertView == null)
вызывается правильно, поскольку записи видны впервые.
Однако, если я прокручиваю ListView
, даже в первый раз, if (convertView == null)
не вызывается. Из-за чего дочерний элемент в верхнем элементе повторяется в первом элементе, который появляется при прокрутке. Насколько я понимаю, если ListView
прокручивается в первый раз, следует вызвать if (convertView == null)
.
Пожалуйста, поправьте меня, если я ошибаюсь.
getView :
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
ViewHolder holder = null;
int rowType = getItemViewType(position);
if (convertView == null) {
holder = new ViewHolder();
switch (rowType) {
case TYPE_ITEM:
convertView = inflater.inflate(R.layout.activity_item, null);
activityModel = (truegroups.model.Activity) arrActivity.get(position);
holder.activityLayout = (RelativeLayout) convertView.findViewById(R.id.activity_item_layout);
holder.startDate = (TextView) convertView.findViewById(R.id.lblStartDate);
holder.line = (TextView) convertView.findViewById(R.id.lblLine);
holder.endDate = (TextView) convertView.findViewById(R.id.lblEndDate);
holder.groupName = (TextView) convertView.findViewById(R.id.lblGroupName);
holder.activity = (TextView) convertView.findViewById(R.id.lblActivity);
holder.location = (TextView) convertView.findViewById(R.id.lblLocation);
holder.topInviteeLayout = (RelativeLayout) convertView.findViewById(R.id.invitationTopPanel);
int iCount = 0;
for (Invitation invitation : activityModel.getArrInvitations()) {
ImageView imgTopStatus = new ImageView(activity);
RelativeLayout.LayoutParams imgStatusParams = new RelativeLayout.LayoutParams(40, 40);
imgStatusParams.setMarginStart(160);
imgStatusParams.setMargins(160, 20, 0, 0);
if (iCount > 0)
imgStatusParams.addRule(RelativeLayout.BELOW, holder.arrImageStatus.get(iCount - 1).getId());
imgTopStatus.setId(iCount * iCount + iCount + 1);
holder.topInviteeLayout.addView(imgTopStatus, imgStatusParams);
holder.arrImageStatus.add(imgTopStatus);
TextView topInviteeName = new TextView(activity);
RelativeLayout.LayoutParams topInviteeParams = new RelativeLayout.LayoutParams(300, ViewGroup.LayoutParams.WRAP_CONTENT);
topInviteeParams.addRule(RelativeLayout.RIGHT_OF, imgTopStatus.getId());
if (iCount > 0)
topInviteeParams.addRule(RelativeLayout.BELOW, holder.arrlblTopInviteeName.get(iCount - 1).getId());
topInviteeParams.setMargins(15, 5, 5, 15);
topInviteeName.setTextColor(Color.BLACK);
topInviteeName.setTextSize(14);
topInviteeName.setBackgroundColor(Color.WHITE);
topInviteeName.setId(iCount * iCount + 2 * iCount + 2);
//topInviteeName.setText(invitation.getChildName());
holder.topInviteeLayout.addView(topInviteeName, topInviteeParams);
holder.arrlblTopInviteeName.add(topInviteeName);
//topInviteeParams.addRule(RelativeLayout.ALIGN_TOP, imgTopStatus.getId());
// imgStatusParams.addRule(RelativeLayout.ALIGN_BASELINE, topInviteeName.getId());
Button decline = new Button(activity);
RelativeLayout.LayoutParams paramsDecline = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 80);
paramsDecline.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
if (iCount > 0)
paramsDecline.addRule(RelativeLayout.BELOW, holder.arrDecline.get(iCount - 1).getId());
decline.setTextColor(Color.BLACK);
decline.setTextSize(9);
decline.setText("Decline");
decline.setId(iCount * iCount + 3 * iCount + 3);
holder.topInviteeLayout.addView(decline, paramsDecline);
holder.arrDecline.add(decline);
Button accept = new Button(activity);
RelativeLayout.LayoutParams paramsAccept = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 80);
if (iCount > 0)
paramsAccept.addRule(RelativeLayout.BELOW, holder.arrAccept.get(iCount - 1).getId());
paramsAccept.addRule(RelativeLayout.RIGHT_OF, topInviteeName.getId());
// paramsAccept.addRule(RelativeLayout.ALIGN_BOTTOM, decline.getId());
paramsAccept.setMarginStart(155);
accept.setTextColor(Color.BLACK);
accept.setTextSize(9);
accept.setId(iCount * iCount + 8 * iCount + 8);
accept.setText("Accept");
holder.topInviteeLayout.addView(accept, paramsAccept);
holder.arrAccept.add(accept);
paramsDecline.addRule(RelativeLayout.ALIGN_TOP, accept.getId());
iCount++;
}
break;
case TYPE_SEPARATOR:
convertView = inflater.inflate(R.layout.activity_header, null);
holder.header = (TextView) convertView.findViewById(R.id.textSeparator);
holder.headerWeek = (TextView) convertView.findViewById(R.id.textWeekNumber);
break;
}
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (arrActivity.size() <= 0) {
Log.d("Data", "No data found !!");
} else {
switch (rowType) {
case TYPE_ITEM:
activityModel = (truegroups.model.Activity) arrActivity.get(position);
//
holder.groupName.setText(activityModel.getGroupName());
holder.activity.setText(activityModel.getTitle());
holder.location.setText(activityModel.getLocation());
holder.startDate.setText(getHours(activityModel.getStartDate()));
holder.endDate.setText(getHours(activityModel.getEndDate()));
int iCount = 0;
for (Invitation invitation : activityModel.getArrInvitations()) {
ImageView imgStatus = holder.arrImageStatus.get(iCount);
//ImageView imgStatus = (ImageView) holder.topInviteeLayout.getChildAt(0);
if (invitation.getResponse().equals("3"))
imgStatus.setImageResource(R.drawable.notresponded);
else if (invitation.getResponse().equals("1"))
imgStatus.setImageResource(R.drawable.attending);
else
imgStatus.setImageResource(R.drawable.decline);
TextView inviteeName = holder.arrlblTopInviteeName.get(iCount);
//TextView inviteeName = (TextView) holder.topInviteeLayout.getChildAt(1);
inviteeName.setText(invitation.getChildName());
iCount++;
}
convertView.setOnClickListener(new OnItemClickListener(position));
break;
case TYPE_SEPARATOR:
holder.header.setText(getHeaderDate(String.valueOf(arrActivity.get(position))));
Calendar calendarGivenDate = Calendar.getInstance();
calendarGivenDate.set(Calendar.DAY_OF_MONTH, Integer.parseInt(String.valueOf(arrActivity.get(position)).split("/")[1]));
calendarGivenDate.set(Calendar.MONTH, Integer.parseInt(String.valueOf(arrActivity.get(position)).split("/")[0]));
calendarGivenDate.set(Calendar.YEAR, Integer.parseInt(String.valueOf(arrActivity.get(position)).split("/")[2]));
int weekNumber = calendarGivenDate.get(Calendar.WEEK_OF_YEAR);
holder.headerWeek.setText(StringUtils.join("WEEK ", String.valueOf(weekNumber)));
break;
}
}
return convertView;
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(activity, e.getMessage(), Toast.LENGTH_LONG).show();
Log.d("Exception : ", e.getMessage());
return convertView;
}
}