简体   繁体   中英

Custom Listview with different row layouts (position problems)

I'm creating custom listview. Everything works fine. But when i launch application problem arrise with layouts. It should be

  • row1
  • row2
  • row1
  • row2

But sometimes it just do something like this

  • row1
  • row2
  • row1
  • row1
  • row2

and so on. Here is the code snippet, where i change row layouts.

View vi=convertView;
    if(convertView==null )
    {
        if(position % 2 == 0)
        vi = inflater.inflate(R.layout.list_row, parent,false);
        else
        vi = inflater.inflate(R.layout.list_row2, parent,false);
    }

    if(position % 3 == 0)
        vi.setBackgroundColor(Color.parseColor("#178C70"));
    else if(position % 3 == 1)
        vi.setBackgroundColor(Color.parseColor("#E51F2A"));
    else vi.setBackgroundColor(Color.parseColor("#1C2023"));

added pictures so you can see my problem.: http://imgur.com/xrwWqBQ,bNE0OHV

It's a bit more complicated that this.

Short answer, in your adapter, please implement these methods:

public int getItemViewType(int position)

public int getViewTypeCount()

Or even better go and read this tutorial http://android.amberfog.com/?p=296

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM