Sort the elements in your adapter in the order in which you want to display them with headings (SectionItem) between the elements.
Create a Person class and a SectionItem class.
An example of an adapter with faces and sections on the first letter of a name:
- A (SectionItem)
- Adam (Person)
- Alex (Person)
- Andre (Person)
- B (SectionItem)
- Ben (Person)
- Boris (Person)
...
adapter.getViewTypeCount return 2.
adapter.getItemViewType(position) 0 SectionItems 1 .
getView (...) SectionItem Person.
:
public class SectionedAdapter extends BaseAdapter {
....
@Override
public int getViewTypeCount() {
return 2;
}
@Override
public int getItemViewType(int position) {
if (getItem(position) instanceof SectionItem){
return 0;
}else{
return 1;
}
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Object item = getItem(position);
if (item instanceof SectionItem) {
if (convertView == null) {
convertView = getInflater().inflate(R.layout.section, null);
}
} else if (item instanceof Person) {
if (convertView == null) {
convertView = getInflater().inflate(R.layout.person, null);
}
}
return convertView;
}
}