JamieB
JamieB

Reputation: 923

Creating a listview with alphabetical sections?

I currently have:

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.grid_contact_item, null);
    }
    BaseContact contact = this.getItem(position);
    TextView text = (TextView) convertView.findViewById(R.id.contact_name);
    if (contact.getName() != null && !contact.getName().equals("")) {
        text.setText(contact.getName());
    }
    ImageView contactIcon = (ImageView) convertView.findViewById(R.id.contact_image);
    if ((contact.getPhotoURI() != null && !contact.getPhotoURI().toString().equals(""))) {
        Picasso.with(this.ctx).load(contact.getPhotoURI()).transform(new CircleTransform()).into(
                contactIcon);
    } else if ((contact.getPhotoURI() == null || contact.getPhotoURI().toString().equals(""))
            && (contact.getPhotoThumbURI() != null && !contact.getPhotoThumbURI().toString()
            .equals(
                    ""))) {
        Picasso.with(this.ctx).load(contact.getPhotoThumbURI()).transform(new CircleTransform()).into(
                contactIcon);
    } else {
        Picasso.with(this.ctx).load(R.drawable.contact_no_picture).transform(new CircleTransform()).into(
                contactIcon);
    }

    return convertView;
}

I would like to replicate what the people app has similar to this: screenshot

Now, judging by this.

I would be able to have a custom view dedicated to just the section, but I don't quite grasp how to figure out whether a position would be a section or not, also if a section is empty, how would it be omitted?

Upvotes: 0

Views: 1126

Answers (1)

dreambit.io dreambitio
dreambit.io dreambitio

Reputation: 1902

About multi rows:

You must override two methods of your adapter:

   @Override
   public int getItemViewType(int position) {
     //your logic. as example:
       return getItem(position).getTitle()==null?0:1
   }

   @Override
   public int getViewTypeCount() {
       return 2;
   }

And then in your getView():

           public View getView(etc...){
              if(convertView == null){
              switch(getItemViewType(position)){
                  //ugly things happen here…
                  case 0: //create view 0 + viewHolder0 and other stuff
                      break;
                  case 1: //create view 1 + viewHolder1 and other stuff
                      break;
              ....
              return covertView;
           }

If you don't wanna write custom adapter just use StickyListHeaders

https://github.com/emilsjolander/StickyListHeaders

Upvotes: 1

Related Questions