Custom rows in ListView with custom Adapter

此生再无相见时 提交于 2019-12-13 04:50:16

问题


I made the following adapter which I used before to create rows that are the same all the time. I removed the creation of the textviews and imageviews.

What I want to achieve is to create different rows depending on the key. A row could contain text and an image while another row only has text. How would I be able to do that?

public class DetailsListAdapter extends ArrayAdapter<ArrayList<String>> {
    private Context context;
    private ArrayList<String> keys;

    public DetailsListAdapter(Context context, ArrayList<String> keys) {
        super(context,R.layout.details); 
        this.context = context;
        this.keys = keys;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v = inflater.inflate(R.layout.details, null);      
        return v;
    }

    @Override
    public int getCount(){
        return keys.size();
    }
}

回答1:


To inflate different layout for rows you need to override getViewItemType and getViewTypeCount.

You should have a look at the video in the link.

http://www.youtube.com/watch?v=wDBM6wVEO70

private static final int TYPE_ITEM1 = 0;
private static final int TYPE_ITEM2 = 1;
private static final int TYPE_ITEM3 = 2; 

Then

int type;
@Override
public int getItemViewType(int position) {

    if (position== 0){
        type = TYPE_ITEM1;
    } else if  (position == 1){
        type = TYPE_ITEM2;
    }
    else
    {
         type= TYPE_ITEM3 ;
    }
    return type;
}

 @Override
 public int getViewTypeCount() {
        return 3; 
 }
@Override  
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
LayoutInflater inflater = null;
int type = getItemViewType(position);
  // instead of if else you can use a case
   if (convertView == null) {
    if (type == TYPE_ITEM1 {
            //infalte layout of type1
           convertView = mInflater.inflate(R.layout.layouttype1, 
                         parent, false);
      }
    if (type == TYPE_ITEM2) {
            //infalte layout of type2
           convertView = mInflater.inflate(R.layout.layouttype2, 
                         parent, false);
    }  else {
            //infalte layout of normaltype
            convertView = mInflater.inflate(R.layout.layouttype3, 
                         parent, false);
 }
 ...// rest of the code
    return convertView;
} 



回答2:


override getViewTypeCount inside this function return the number of types views you want to use inside listview.

Override getItemViewType(int position) inside this write your logic to get the type of view.

@Override
public int getViewTypeCount() {
   return 2; //return 2, in case you have two types of view
}

@Override
public int getItemViewType(int position) {
   return (contition) ? R.layout.layout1 : R.layout.layout2; //return 2, in case you have two types of view
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    convertView = (convertView == null) ?View.inflate(context, getItemViewType(position), null) : convertView;
    return convertView;
}


来源:https://stackoverflow.com/questions/20517922/custom-rows-in-listview-with-custom-adapter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!