-1

I would like to create a listview that will have different custom layouts. Is it possible ? If yes, I would like to know how it's done.

DeyaEldeen
  • 10,847
  • 10
  • 42
  • 75
razer_gh
  • 45
  • 2
  • 10

1 Answers1

0

You can use the idea of this question

I mark it as dupliacated... For any case, you can change your adater as below:

Implement (override) following method on your adapter:

int getViewTypeCount () {
   //return  the number of layouts that you have
   return 2;
}

int getItemViewType(int position) {
    //return the view type taht you want for each position...
    // for example, type 0 for position 0, 1,2, 3
    // type 1 for position 4 5 6
    if ( position >= 0 && position <= 3)
        return 0;
    else
        return 1;
}

View getView(int position, View convertView, ViewGroup parent) {
     if (convertView == null) {
         // inflate layout acordind the view type
         // for type 0, inflate layout 1
         // for type 1, inflate layout 2
     }
     ....
     return convertView;
}

Android will re-use the views. However, it is smart enough to re-use views of the same type.

In my app for example, i have this to show a image when list is empty.. When the list has items, I show the list of items... Similar to Google Messaging app when the list is empty

Community
  • 1
  • 1
guipivoto
  • 18,327
  • 9
  • 60
  • 75