adapter实现多布局

在adapter中添加这两个方法
public int getViewTypeCount() {
    return 2;
}

@Override
public int getItemViewType(int position) {
    int p = position % 2;
    if (p == 0) {
        return a = 0;
    } else {
        return a = 1;
    }
}
 
  
 
  
public View getView(int i, View view, ViewGroup viewGroup) {
    ViewHolder holder;
    int type = getItemViewType(i);
    if (view == null) {
        holder = new ViewHolder();
        switch (type) {
            case 0:
                view = View.inflate(getActivity(), R.layout.xlv2_item, null);
                holder.tv = view.findViewById(R.id.tv);
                holder.img = view.findViewById(R.id.img);
                break;
            case 1:
                view = View.inflate(getActivity(), R.layout.xlv_item, null);
                holder.tv = view.findViewById(R.id.tv);
                holder.img = view.findViewById(R.id.img);
                break;
        }
        view.setTag(holder);
    } else {
        holder = (ViewHolder) view.getTag();
    }

    ImageLoader.getInstance().displayImage(list.get(i).thumbnail_pic_s,holder.img);
    holder.tv.setText(list.get(i).title);
    return view;
}
 
  
class ViewHolder {
    public ImageView img;
    public TextView tv;
}
如此简单 如此美妙
 
  
 
  
 
  
 
 

你可能感兴趣的:(adapter实现多布局)