2014-07-15 53 views
0

我正在開發一個Android應用程序,它使用ListView來顯示用戶及其配置文件。您可以在下面的圖片上看到它的外觀。有可能有大量的用戶,所以我實現了延遲加載功能,一次加載四個用戶。當應用程序啓動時(列表中有4個用戶),一切看起來都很棒,但是當我向下滾動並加載下四個用戶後,發生了一些奇怪的事情。正如你可以在圖像上看到的,每個用戶都有他的棋子,例如他們中的一些人有眼鏡,而其中一些人不是。滾動後,似乎新加載的用戶或其中一些用戶也有眼鏡,儘管他們不應該擁有眼鏡。ListView回收視圖功能影響列表中的其他項目

http://pasteboard.co/1xs78l6C.png

我可以猜測,發生問題,因爲適配器回收的意見。這些視圖由許多圖像組成,我認爲在沒有此功能的情況下實施適配器(回收)並不是一個好主意。另一方面,我對此並不熟悉,我不知道應該在哪裏注意防止這種行爲。我將不勝感激任何幫助或建議如何解決這個問題。

在這裏你可以看到我的適配器看起來像

public class UserAdapter extends BaseAdapter { 

    private final Context context; 
    private ArrayList<User> users; 

    private HashMap<String, Integer> resIds; 

    private final int[] slotIds = { R.id.imgRowSlot1, R.id.imgRowSlot2, 
      R.id.imgRowSlot3 }; 

    public UserAdapter(Context context) { 
     this.context = context; 
     users = new ArrayList<User>(); 
     resIds = new HashMap<String, Integer>(Config.init()); 
    } 

    public void updateUsers(List<User> users) { 
     this.users = new ArrayList<User>(users); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public int getCount() { 
     return users.size(); 
    } 

    @Override 
    public User getItem(int position) { 
     return users.get(position); 
    } 

    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

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

     TextView txtFirstName; 
     TextView txtLastName; 

     ImageView imgSlot1; 
     ImageView imgSlot2; 
     ImageView imgSlot3; 

     ImageView imgHead; 
     ImageView imgGlasses; 
     ImageView imgTop; 
     ImageView imgBottom; 

     if (convertView == null) { 
      convertView = LayoutInflater.from(context).inflate(
        R.layout.user_row, parent, false); 

      txtFirstName = (TextView) convertView 
        .findViewById(R.id.txtFirstName); 
      txtLastName = (TextView) convertView.findViewById(R.id.txtLastName); 

      imgSlot1 = (ImageView) convertView.findViewById(R.id.imgRowSlot1); 
      imgSlot2 = (ImageView) convertView.findViewById(R.id.imgRowSlot2); 
      imgSlot3 = (ImageView) convertView.findViewById(R.id.imgRowSlot3); 

      imgHead = (ImageView) convertView 
        .findViewById(R.id.imgHead_adapter); 
      imgGlasses = (ImageView) convertView 
        .findViewById(R.id.imgGlasses_adapter); 
      imgTop = (ImageView) convertView.findViewById(R.id.imgTop_adapter); 
      imgBottom = (ImageView) convertView 
        .findViewById(R.id.imgBottom_adapter); 

      convertView.setTag(new ViewHolder(txtFirstName, txtLastName, 
        imgSlot1, imgSlot2, imgSlot3, imgHead, imgGlasses, imgTop, 
        imgBottom)); 

     } else { 

      ViewHolder viewHolder = (ViewHolder) convertView.getTag(); 
      txtFirstName = viewHolder.txtFirstName; 
      txtLastName = viewHolder.txtLastName; 

      imgSlot1 = viewHolder.imgSlot1; 
      imgSlot2 = viewHolder.imgSlot2; 
      imgSlot3 = viewHolder.imgSlot3; 

      imgHead = viewHolder.imgHead; 
      imgGlasses = viewHolder.imgGlasses; 
      imgTop = viewHolder.imgTop; 
      imgBottom = viewHolder.imgBottom; 
     } 

     User user = getItem(position); 

     int[] slots = user.getSlots(); 

     // name 
     txtFirstName.setText(user.getFirstName()); 
     txtLastName.setText(user.getLastName()); 

     // fruits 
     imgSlot1.setImageResource(MyDataController.fruits[slots[0]]); 
     imgSlot2.setImageResource(MyDataController.fruits[slots[1]]); 
     imgSlot3.setImageResource(MyDataController.fruits[slots[2]]); 
     FruitExtractor.setAlpha(convertView, slotIds, user); 

     // pawn head 
     if (user.getConsCounter() >= 2) { 
      imgHead.setImageResource(R.drawable.head_happy_noeyes); 
     } else if (user.getConsCounter() == 1) { 
      imgHead.setImageResource(R.drawable.head_indiferent_noeyes); 
     } else { 
      imgHead.setImageResource(R.drawable.head_sad_noeyes); 
     } 

     // glasses 
     if (user.getGlasses() != null) { 
      imgGlasses.setImageResource(resIds.get(user.getGlasses() 
        .getResIdName())); 
     } 

     // body 
     imgTop.setImageResource(resIds.get(user.getTop().getResIdName())); 
     imgBottom.setImageResource(resIds.get(user.getBottom().getResIdName())); 

     return convertView; 
    } 

    private static class ViewHolder { 

     public final TextView txtFirstName; 
     public final TextView txtLastName; 

     public final ImageView imgSlot1; 
     public final ImageView imgSlot2; 
     public final ImageView imgSlot3; 

     public final ImageView imgHead; 
     public final ImageView imgGlasses; 
     public final ImageView imgTop; 
     public final ImageView imgBottom; 

     public ViewHolder(TextView txtFirstName, TextView txtLastName, 
       ImageView imgSlot1, ImageView imgSlot2, ImageView imgSlot3, 
       ImageView imgHead, ImageView imgGlasses, ImageView imgTop, 
       ImageView imgBottom) { 
      this.txtFirstName = txtFirstName; 
      this.txtLastName = txtLastName; 
      this.imgSlot1 = imgSlot1; 
      this.imgSlot2 = imgSlot2; 
      this.imgSlot3 = imgSlot3; 
      this.imgHead = imgHead; 
      this.imgGlasses = imgGlasses; 
      this.imgTop = imgTop; 
      this.imgBottom = imgBottom; 
     } 
    } 

    public List<User> getUsers() { 
     return users; 
    } 
} 

回答

1
// glasses 
    if (user.getGlasses() != null) { 
     imgGlasses.setImageResource(resIds.get(user.getGlasses() 
       .getResIdName())); 
    }else{ 
     //there is no else statement to clear the previous glasses 
     //you can do something like this 
     imgGlasses.setImageResource(android.R.color.transparent); 
    } 
+0

我加入這個'其他{imgGlasses.setImageDrawable(NULL); }'在我看來,它工作正常。 Thx的小費! – amsalk

+0

@ am.sal很高興。爲了避免將來出現這樣的錯誤,請創建一個方法ViewHolder.clear(),它可以在設置它們之前清除所有視圖。 – Olayinka

相關問題