多条目加载

 
  
class MyAdapter extends BaseAdapter{
    List list;
    Context context;
    private int TitleView=0;//只有标题
    private int LeftView=1;//图片在左
    private int RightView=2;//图片在右
    private int BottomView=3;//图片在下
    public MyAdapter(List list, Context context) {
        this.list=list;
        this.context=context;
    }


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

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
    //数量
    @Override
    public int getViewTypeCount() {
        return 4;
    }
    //类型
    @Override
    public int getItemViewType(int position) {
        if(position%4==0){
            return TitleView;
        }else if(position%4==1){
            return LeftView;
        }else if(position%4==2){
            return RightView;
        }
        return BottomView;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        //判断
        if(getItemViewType(position)==TitleView){
            TitleHolder titleHolder;
            if (convertView==null){
                convertView=View.inflate(context,R.layout.title_layout,null);
                titleHolder=new TitleHolder();
                titleHolder.des= (TextView) convertView.findViewById(des);
                convertView.setTag(titleHolder);
            }else{
                titleHolder= (TitleHolder) convertView.getTag();
            }
            titleHolder.des.setText(list.get(position).getTitle());
        }else if(getItemViewType(position)==LeftView){
            LeftHolder leftHolder;
            if (convertView==null){
                convertView=View.inflate(context,R.layout.left_layout,null);
                leftHolder=new LeftHolder();
                leftHolder.des= (TextView) convertView.findViewById(des);
                leftHolder.img= (ImageView) convertView.findViewById(R.id.img);
                convertView.setTag(leftHolder);
            }else{
                leftHolder= (LeftHolder) convertView.getTag();
            }
            leftHolder.des.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getFirstImg(),leftHolder.img,ImageUtil.getDefaultOption());
        }else if(getItemViewType(position)==RightView){
            RightHolder rightHolder;
            if (convertView==null){
                convertView=View.inflate(context,R.layout.right_layout,null);
                rightHolder=new RightHolder();
                rightHolder.des= (TextView) convertView.findViewById(des);
                rightHolder.img= (ImageView) convertView.findViewById(R.id.img);
                convertView.setTag(rightHolder);
            }else{
                rightHolder= (RightHolder) convertView.getTag();
            }
            rightHolder.des.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getFirstImg(),rightHolder.img,ImageUtil.getDefaultOption());
        }else if(getItemViewType(position)==BottomView){
            BottomHolder bottomHolder;
            if (convertView==null){
                convertView=View.inflate(context,R.layout.bottom_layout,null);
                bottomHolder=new BottomHolder();
                bottomHolder.des= (TextView) convertView.findViewById(des);
                bottomHolder.img= (ImageView) convertView.findViewById(R.id.img);
                convertView.setTag(bottomHolder);
            }else{
                bottomHolder= (BottomHolder) convertView.getTag();
            }
            bottomHolder.des.setText(list.get(position).getTitle());
            ImageLoader.getInstance().displayImage(list.get(position).getFirstImg(),bottomHolder.img,ImageUtil.getDefaultOption());
        }
        return convertView;
    }
    class TitleHolder{
        TextView des;
    }
    class LeftHolder{
        TextView des;
        ImageView img;
    }
    class RightHolder{
        TextView des;
        ImageView img;
    }
    class BottomHolder{
        TextView des;
        ImageView img;
    }
}

你可能感兴趣的:(多条目加载)