-2

我遇到了观点被回收的问题。我正在使用 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;
    }
 }
4

1 回答 1

2

您必须实现不同的逻辑。您的代码的问题是,如果convertView不为空,这意味着它已被回收,但它之前膨胀了与您的数据相关的不同视图,它将显示旧回收视图的布局(另一个布局,而不是此数据集所需的布局)。

所以你只需要检查,如果回收的视图膨胀了布局,你需要这个特定的行,或者它是否膨胀了另一个视图。如果是前者,那么你可以只设置你的数据,如果不是,那么你必须膨胀正确的视图。

于 2013-10-15T02:10:00.013 回答