我遇到了观点被回收的问题。我正在使用 2 种布局,但是当我向上和向下滚动时,有些行会改变布局。我尝试了其他教程但收效甚微。有什么建议么?
public class CustomAdapter extends BaseAdapter {
Date date = new Date();
private OfferList[] offers;
private String nameCompare = "";
LayoutInflater inflater = getLayoutInflater();
CustomAdapter (OfferList[] offers, FetchItemsTask fetchItemsTask) {
this.offers = offers;
}
@Override
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
if (convertView == null) {
holder = new ViewHolder();
if (getItemViewType(position) == 0) {
convertView = inflater.inflate(R.layout.list_offer_group, null);
}
else {
nameCompare = offers[position].getName();
convertView = inflater.inflate(R.layout.list_offer, null);
}
holder.name = (TextView) convertView.findViewById(R.id.name);
holder.color = (TextView) convertView.findViewById(R.id.car_color_stock);
holder.time = (TextView) convertView.findViewById(R.id.time);
holder.offerStatus = (TextView) convertView.findViewById(R.id.offer_status);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
holder.name.setText(offers[position].getCustomerName());
holder.carStockColor.setText(offers[position].getVehicleYear() + " " + offers[position].getVehicleMake());
java.util.Date time1 = new java.util.Date(Long.parseLong(offers[position].getModified()));
holder.time.setText(time1.toString().substring(0, 11) + " | ");
holder.offerStatus.setText(offers[position].getDealStatus());
return convertView;
}
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (nameCompare.equalsIgnoreCase(offers[position].getCustomerName())) {
return 0;
}
else {
return 1;
}
}
@Override
public int getCount() {
return offers.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
public class ViewHolder {
public int type;
TextView name;
TextView color;
TextView time;
TextView offerStatus;
}
}