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

点点圈 提交于 2019-12-03 02:58:28

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, 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;
}

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.

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;
    }
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!