滚动时列表视图中的项目顺序错误

时间:2014-10-22 07:34:07

标签: java android eclipse listview scroll

我的布局如下所示:

enter image description here

在listview中有几个CustomView。项目根据其索引显示。

我使用此代码:

@Override
public int getItemViewType(int position) {
    return UL.separators.contains(position) ? 1 : 0;
}

这是getView功能:

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

    NewsHolder holder = null;
       View row = convertView;
        holder = null;
        int type = getItemViewType(position); 
        if(row == null)
        {
            LayoutInflater inflater = ((Activity)context).getLayoutInflater();

            holder = new NewsHolder();

            switch (type) {
            case 1:

                row= inflater.inflate(R.layout.section_header, null);
                holder.itemName = (TextView)row.findViewById(R.id.header);
                holder.itemName.setText("CUSTOM TEXT");
                row.setTag(holder);
                break;
            case 0:
                row= inflater.inflate(R.layout.custom_row, null);

                holder.itemName = (TextView)row.findViewById(R.id.example_itemname);
                holder.icon=(ImageView)row.findViewById(R.id.example_image);
                holder.button3=(Button)row.findViewById(R.id.swipe_button3);
                row.setTag(holder);
                ItemRow itemdata = data.get(position);
                holder.itemName.setText(itemdata.getItemName());
                holder.icon.setImageDrawable(itemdata.getIcon());

                break;
        }

        }
        else
        {
           holder = (NewsHolder)row.getTag();
        }

        return row;

但是当我滚动listview时,元素会随机排列,如下面的屏幕截图所示。

滚动前:

enter image description here

滚动后:

enter image description here

1 个答案:

答案 0 :(得分:1)

试试这个

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

    NewsHolder holder = null;
   LayoutInflater inflater = ((Activity)context).getLayoutInflater();
    int type = getItemViewType(position);

    switch (type) {
        case 1: {
            holder = new NewsHolder();
            if (convertView == null || (((NewsHolder) convertView.getTag()).getType() == 0)) {
                convertView = inflater.inflate(R.layout.section_header, null);
                holder.itemName = (TextView) convertView.findViewById(R.id.header);
                holder.setType(1);
                convertView.setTag(holder);
            } else {
                holder = (NewsHolder) convertView.getTag();
            }
            holder.itemName.setText("CUSTOM TEXT");

            break;
        }
        case 0: {
            holder = new NewsHolder();
            if (convertView == null || (((NewsHolder) convertView.getTag()).getType() == 1)) {
                convertView = inflater.inflate(R.layout.custom_row, null);
                holder.itemName = (TextView) convertView.findViewById(R.id.example_itemname);
                holder.icon = (ImageView) convertView.findViewById(R.id.example_image);
                holder.button3 = (Button) convertView.findViewById(R.id.swipe_button3);
                holder.setType(0);
                convertView.setTag(holder);
            } else {
                holder = (NewsHolder) convertView.getTag();
            }
            ItemRow itemdata = data.get(position);
            holder.itemName.setText(itemdata.getItemName());
            holder.icon.setImageDrawable(itemdata.getIcon());

            break;
        }
    }


    return convertView;
}

然后将此添加到NewsHolder类

 private class NewsHolder {

    private int holderType;

    public void setType(int holderType){
        this.holderType=holderType;
    }
    public int getType(){
        return this.holderType;
    }

    // YOUR VARIABLES (VIEWS)
    // --
    // ВАШИ ПЕРЕМЕННЫЕ ВЬЮ
}