多条目listview——myadapter

package com.example.com.zuoyelmh;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.ImageScaleType;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.List;

/**
 * Created by linmeihui on 2018/1/3.
 */

class MyAdapter extends BaseAdapter {

    List data;
    Context context;

    private int ONE_IMAGE = 1;
    private int TWO_IMAGE = 2;
    private int THREE_IMAGE = 3;
    private DisplayImageOptions options;

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

        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)//使用内存缓存
                .cacheOnDisk(true)//使用磁盘缓存
                .showImageOnLoading(R.mipmap.ic_launcher)//设置正在下载的图片
                .showImageForEmptyUri(R.mipmap.ic_launcher)//url为空或请求的资源不存在时
                .showImageOnFail(R.mipmap.ic_launcher)//下载失败时显示的图片
                .bitmapConfig(Bitmap.Config.RGB_565)//设置图片色彩模式 1px=2字节
                .imageScaleType(ImageScaleType.EXACTLY)//设置图片的缩放模式
                .displayer(new RoundedBitmapDisplayer(100))//设置圆角 30代表半径 自定义
                .build();

    }

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

    @Override
    public int getItemViewType(int position) {

        String pics = data.get(position).getThumbnail_pic_s();
        String pics02 = data.get(position).getThumbnail_pic_s02();
        String pics03 = data.get(position).getThumbnail_pic_s03();

        if (pics != null && pics02 == null && pics03 == null) {
            return ONE_IMAGE;
        } else if (pics != null && pics02 != null && pics03 == null) {
            return TWO_IMAGE;

        } else {
            return THREE_IMAGE;
        }

    }

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

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        int type = getItemViewType(i);
        if (type == ONE_IMAGE) {
            ViewHolder1 holder1;
            if (view == null) {
                holder1 = new ViewHolder1();
                view = View.inflate(context, R.layout.oneimg, null);
                holder1.tv = view.findViewById(R.id.tv);
                holder1.img = view.findViewById(R.id.img);
                view.setTag(holder1);
            } else {
                holder1 = (ViewHolder1) view.getTag();
            }
            holder1.tv.setText(data.get(i).getTitle());
            ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s(),holder1.img,options);

            return view;
        } else if (type == TWO_IMAGE) {
            ViewHolder2 holder2;
            if (view == null) {
                holder2 = new ViewHolder2();
                view = View.inflate(context, R.layout.twoimg, null);
                holder2.tv = view.findViewById(R.id.tv);
                holder2.img1 = view.findViewById(R.id.img1);
                holder2.img2 = view.findViewById(R.id.img2);
                view.setTag(holder2);
            } else {
                holder2 = (ViewHolder2) view.getTag();
            }
            holder2.tv.setText(data.get(i).getTitle());
            ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s02(),holder2.img1,options);
            ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s02(),holder2.img2,options);
            return view;
        } else   {
            ViewHolder3 holder3;
            if (view == null) {
                holder3 = new ViewHolder3();
                view = View.inflate(context, R.layout.threeimg, null);
                holder3.tv = view.findViewById(R.id.tv);
                holder3.img1 = view.findViewById(R.id.img1);
                holder3.img2 = view.findViewById(R.id.img2);
                holder3.img3 = view.findViewById(R.id.img3);
                view.setTag(holder3);
            } else {
                holder3 = (ViewHolder3) view.getTag();
            }
            holder3.tv.setText(data.get(i).getTitle());
            ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s03(),holder3.img1,options);
            ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s03(),holder3.img2,options);
            ImageLoader.getInstance().displayImage(data.get(i).getThumbnail_pic_s03(),holder3.img3,options);
            return view;
        }

    }


class ViewHolder1{
        TextView tv;
        ImageView img;
}
class ViewHolder2{
        TextView tv;
        ImageView img1,img2;
    }
class ViewHolder3{
        TextView tv;
        ImageView img1,img2,img3;
    }
}

你可能感兴趣的:(多条目listview——myadapter)