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

前端 未结 3 1176
花落未央
花落未央 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;
    }
    
    0 讨论(0)
  • 2021-02-06 04:02

    When you have several types of views that should be recycled you have to tell the list view how many types you have got by implement the method

    @Override
        public int getViewTypeCount() {
            return 7;
        }
    
    0 讨论(0)
  • 2021-02-06 04:20

    You need to call bindView() always. The idea or reuse is as following. If convertView is null, you create and initialize a new view. If convertView is not null, you take this view and convert it to be new view, meaning you call bindView() with convertView instance.

    Checkout this Javadoc for more details.

    0 讨论(0)
提交回复
热议问题