适配器 多条目加载

需要使用两个方法: 
多条目的类型 getItemViewType()  类型的数量 getViewTypeCount()

/**
 * Created by dell on 2017/12/9.
 */

public class MyAdapter extends BaseAdapter {
    Context context;
    List list;

    public MyAdapter(Context context, List list) {
        this.context = context;
        this.list = list;
    }

  @Override
    public int getItemViewType(int position) {
 
  
//判断图片
 /*
     String pic = list.get(position).getThumbnail_pic_s02();
     if (TextUtils.isEmpty(pic)){
        //一张图片的
        return 0;
     }else{
       //三张图片的
         return 1;
     }*/
 
 List images = list.get(position).getImages(); if (null!=images&&!"".equals(images)){ if(images.size()==1){ return 1; } } return 0; } @Override public int getViewTypeCount() { return 2; } @Override public int getCount() { return list.size(); } @Override public Object getItem(int i) { return null; } @Override public long getItemId(int i) { return 0; } @Override public View getView(int i, View view, ViewGroup viewGroup) { int b = getItemViewType(i); if (b==0){ if (view==null){ view=View.inflate(context,R.layout.rement_xlistview_item,null); } TextView item1_tv1 = (TextView) view.findViewById(R.id.item1_tv1); TextView item1_tv2 = (TextView) view.findViewById(R.id.item1_tv2); item1_tv1.setText(list.get(i).getDesc()); item1_tv2.setText(list.get(i).getCreatedAt()); return view; }else if (b==1){ view=View.inflate(context,R.layout.remen_xlistview_item2,null); } TextView item2_tv1 = (TextView) view.findViewById(R.id.item2_tv1); TextView item2_tv2 = (TextView) view.findViewById(R.id.item2_tv2); ImageView item2_img = (ImageView) view.findViewById(R.id.item2_img); item2_tv1.setText(list.get(0).getDesc()); item2_tv2.setText(list.get(0).getCreatedAt()); ImageLoader.getInstance().displayImage(list.get(i).getImages().get(0),item2_img); return view; }}

 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
  
 
 

你可能感兴趣的:(java)