0

I have a layout as shown below:

enter image description here

In listview there are several CustomViews. Items are displayed according to their indices.

I use this code:

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

This is getView func:

@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;

But when I scrol listview, elements are arranged at random, as shown on screenshots below.

Before scroll:

enter image description here

After scroll:

enter image description here

Aleks G
  • 56,435
  • 29
  • 168
  • 265
SSHEX
  • 27
  • 1
  • 9
  • You should probably read about views recycling ... [This question](http://stackoverflow.com/questions/11945563/how-listviews-recycling-mechanism-works) could be a good start. – 2Dee Oct 22 '14 at 07:40
  • Personally, instead of making dummy items in a list and then filling them, I'd call the list and populate it with items generated from code. That way you know you control the ordering. It could also be that the data you are providing is not ordered like you think it is. – G_V Oct 22 '14 at 07:41
  • Disregard what I said above, maybe https://www.youtube.com/watch?v=N6YdwzAvwOA helps. It's the guy who wrote ListViews explaining how the work, do's and don'ts. – G_V Oct 22 '14 at 07:57
  • This answer help to me http://stackoverflow.com/questions/15844338/custom-listview-shows-less-items-in-random-order You must to bind data in GetView Like this before "return row": `// Bind data to views, depending on data type if(type==0) { ItemRow itemdata = data.get(position); holder.itemName.setText(itemdata.getItemName()); holder.icon.setImageDrawable(itemdata.getIcon()); } else { holder.itemName = (TextView)row.findViewById(R.id.header); holder.itemName.setText("ТЫ ГДЕ?"); } return row;` – SSHEX Oct 22 '14 at 08:22

1 Answers1

1

Try this

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

Then add this to your NewsHolder class

 private class NewsHolder {

    private int holderType;

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

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