Getview parameter “convertview” not null on new “position” parameter

前端 未结 3 1177
花落未央
花落未央 2021-02-06 03:49

I\'m using an ArrayAdapter for a list of my own type of objects (only one type) and I give the user an option to create more items (thus creating more views for those items). At

3条回答
  •  花落未央
    2021-02-06 03:58

    A friend explained the problem to me and now it seems to work. Basically ListView only holds a small number of views and recycles them all the time. In my case I have a Nexus 4 and so it seems to have 7 views total because the 8th was always the one who started to cause trouble. What I was missing in my getView() was a condition checking for correlation between the position and the ID of the current item within the ArrayAdapter. Here is how it looks now that it works:

    @Override
    public View getView(int position, @Nullable View convertView, ViewGroup parent) {
        View v;
        PreviewItemHolder holder = null;
    
        // Initialize view if convertview is null
        if (convertView == null) {
            v = newView(parent, position);
        }
        // Populate from previously saved holder
        else {
            // If position and id of set do not match, this view needs to be re-created, not recycled
            if (((PreviewItemHolder) convertView.getTag()).set.getId() != position) {
                v = newView(parent, position);
            }
            else {
                // Use previous item if not null
                v = convertView;
    
                // Get holder
                holder = (PreviewItemHolder) v.getTag();
            }
        }
    
        // Populate if the holder is null (newly inflated view) OR
        // if current view's holder's flag is true and requires populating
         if (holder == null || holder.readPopulateFlag()) {
             bindView(position, v);
         }
    
        return v;
    }
    

提交回复
热议问题