2014-06-09 32 views
0

我有一個行列表,每行都有一個Meals列表。什麼是填充我的ListView的最佳方式?這就是它看起來像現在:http://imgur.com/dMEcVUF這是我的適配器:Android:性能問題填充我的(複雜)ListView

public CanteenPlanAdapter(Context context, Plan plan) { 
    this.context = context; 
    if (plan != null) lines = plan.getLines(); 
} 

@Override 
public Object getItem(int position) { 
    if (lines != null) return lines.get(position); 
    else return null; 
} 

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    Line line = (Line) getItem(position); 

    List<Meal> meals = line.getMeals(); 

    LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    convertView = inflater.inflate(R.layout.canteen_list_group_item, parent, false); 

    for (int i = 0; i < meals.size(); i++) { 

     Meal meal = meals.get(i); 

     View child = inflater.inflate(R.layout.canteen_list_child_item, null); 

     TextView name = (TextView) child.findViewById(R.id.canteen_meal_name); 
     name.setText(meal.getMeal()); 

     TextView price = (TextView) child.findViewById(R.id.canteen_meal_price); 
     price.setText(String.valueOf(meal.getPrice1()) + " €"); 

     if (meal.isBio()) { 
      ImageView bio = (ImageView) child.findViewById(R.id.meal_bio); 
      bio.setVisibility(1); 
     } 
     if (meal.isCow()) { 
      ImageView cow = (ImageView) child.findViewById(R.id.meal_cow); 
      cow.setVisibility(1); 
     } 
     if (meal.isCow_aw()) { 
      ImageView cow_aw = (ImageView) child.findViewById(R.id.meal_cow_aw); 
      cow_aw.setVisibility(1); 
     } 
     if (meal.isFish()) { 
      ImageView fish = (ImageView) child.findViewById(R.id.meal_fish); 
      fish.setVisibility(1); 
     } 
     if (meal.isPork()) { 
      ImageView pork = (ImageView) child.findViewById(R.id.meal_pork); 
      pork.setVisibility(1); 
     } 
     if (meal.isVeg()) { 
      ImageView veg = (ImageView) child.findViewById(R.id.meal_veg); 
      veg.setVisibility(1); 
     } 
     if (meal.isVegan()) { 
      ImageView vegan = (ImageView) child.findViewById(R.id.meal_vegan); 
      vegan.setVisibility(1); 
     } 
     ((ViewGroup) convertView).addView(child); 
    } 
    TextView name = (TextView) convertView.findViewById(R.id.canteen_line_name); 
    convertView.setTag(name); 

    name.setText(line.getName()); 

    return convertView; 
} 

它的工作,但表現顯然很糟糕......我試着用Viewholder,但我不知道如何添加膳食列表項目..

回答

0

要在ListView中獲得可接受的性能,您必須必須重用項目視圖。在您滾動時創建和銷燬它們會使性能受到很大影響。因此,第一部分使用提供的convertView,如果它不爲null(ViewHolder模式是對此的進一步優化)。

在這種情況下,額外的困難似乎在於,每個列表項目根據成分的不同也有不同數量的孩子。你應該做的是重複使用,如果你需要的比你已經擁有的更多(並且將其他設置爲GONE),那麼只能創建更多的子視圖。

粗糙例如(可能無法編譯,僅作爲指導原則使用):)

if (convertView == null) 
    convertView = inflate(R.layout.canteen_list_group_item); 

ViewGroup group = (ViewGroup)convertView; 

for (int i = 0; i < meals.size(); i++) 
{ 
    View child; 
    if (group.getChildCount() > i) 
    { 
     // We have enough views already, recycle one. 
     child = group.getChildAt(i); 
    } 
    else 
    { 
     // We need an extra row. 
     child = inflate(...); 
     group.addView(child); 
    } 

    setVisibility(View.VISIBLE); // in case it was hidden previously. 

    // Load this child 
    TextView name = (TextView) child.findViewById(R.id.canteen_meal_name); 
    <etc, same as before> 
} 

// Hide extra children 
for (int i = meals.size(); i < group.getChildCount(); i++) 
    group.getChildAt(i).setVisibility(View.GONE); 

return group; 

當然,索引可以必須被調整(即,不是從0開始),如果canteen_list_group_item具有其他的「固定「孩子的意見,但我希望你明白。

0

首先感謝您的小雞響應!我根據你的想法改變了我的代碼,它正在工作(有點)。 canteen_list_group_item有2個固定子視圖(名稱和分隔線),所以我調整了指標:

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 

    Line line = (Line) getItem(position); 
    List<Meal> meals = line.getMeals(); 

    if (convertView == null) { 
     convertView = LayoutInflater.from(context).inflate(R.layout.canteen_list_group_item, parent, false); 
    } 
    ViewGroup group = (ViewGroup) convertView; 

    for (int i = group.getChildCount(); i < meals.size() + 2; i++) { 

     Meal meal = meals.get(i - 2); 

     View child; 
     if (group.getChildCount() > i) { 
      child = group.getChildAt(i); 
     } else { 
      child = LayoutInflater.from(context).inflate(R.layout.canteen_list_child_item, null); 
      group.addView(child); 
     } 
     child.setVisibility(View.VISIBLE); 

     TextView name = (TextView) child.findViewById(R.id.canteen_meal_name); 
     name.setText(meal.getMeal()); 

     TextView price = (TextView) child.findViewById(R.id.canteen_meal_price); 
     price.setText(String.valueOf(meal.getPrice1()) + " €"); 

    } 
    for (int i = meals.size() + 2; i < group.getChildCount(); i++) { 
     group.getChildAt(i).setVisibility(View.GONE); 
    } 

    TextView name = (TextView) convertView.findViewById(R.id.canteen_line_name); 
    convertView.setTag(name); 

    name.setText(line.getName()); 

    return convertView; 
} 

我的問題是現在的子項似乎隨機出現或在他們的小組並沒有消失,是「爲了」像它應該。我的代碼有錯誤還是錯過了某些東西?