2013-10-15 77 views
-2

我遇到了回收视图的问题。我使用2个布局,但是当我上下滚动时,某些行会更改布局。我已经尝试了其他教程,但很少成功。有什么建议么?ListView回收视图,多个布局

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; 
    } 
} 
+0

你已经张贴了这个问题 – tyczj

+0

是啊,我没有得到一个很好的答案 – ono

+0

所以你不要创建一个新的问题,那就是同样的事情 – tyczj

回答

2

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

所以你只需要检查一下,如果再循环视图夸大了布局,那么你需要这个特定的行,或者如果它膨胀了另一个视图。如果前者是这种情况,那么你可以设置你的数据,如果没有,那么你必须夸大正确的观点。

+0

是的我想这样做只是通过膨胀只有一个布局 – ono

+0

我将如何检查回收的视图充气布局或如果它夸大了另一种观点? – ono