2014-07-01 19 views
0

我目前有:用字母順序創建列表視圖?

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

我想重複一下人們的應用程序有與此類似: screenshot

現在,this判斷。

我可以有一個自定義的視圖專用於該部分,但我不太清楚如何確定一個位置是否是一個部分,如果一個部分是空的,它將如何被省略?

回答

1

關於多行:

必須覆蓋適配器的兩種方法:)

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

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

,然後在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; 
      } 

如果你不想寫自定義適配器只是使用StickyListHeaders

https://github.com/emilsjolander/StickyListHeaders

+0

但是,我知道如何將標題置於x位置以顯示新信件的開頭? – JamieB

+0

將字段添加到您的BaseContact類 –

+0

這將如何幫助?我已經知道我想添加的人的名字,我需要知道如何以編程方式決定是否應該添加標題或項目。 – JamieB