多条目展示

多条目展示主要就是适配器


public class MyAdapter extends BaseAdapter{
    private Context mContext;
    private List<Data.ListBean> list

    ;定义变量,要从0开始

    private final int TYPE1 = 0;
    private final int TYPE2 = 1;
    private ImageLoader mImageLoader;
    private DisplayImageOptions mOptions;

    public MyAdapter(Context context, List<Data.ListBean> list) {
        mContext = context;
        this.list = list;

        mImageLoader=ImageLoader.getInstance();
        File file= new File(Environment.getExternalStorageDirectory(),"Bwei");
        if(!file.exists())
            file.mkdirs();

        ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(context)
                .diskCache(new UnlimitedDiskCache(file))
                .build();

        mImageLoader.init(configuration);

        mOptions=new DisplayImageOptions.Builder()
                .showImageOnLoading(R.mipmap.ic_launcher)
                .cacheOnDisk(true)
                .build();
    }

    @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 getItemViewType(int position) {
        return list.get(position).getType()==1?TYPE1:TYPE2;
    }

一共有多少种类

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder=null;
        int type=getItemViewType(position);

        if(convertView==null){


判断加载那个布局,赋值

            holder=new ViewHolder();
            if(type==TYPE1){
                convertView=View.inflate(mContext,R.layout.item1,null);
                holder.id=(TextView)convertView.findViewById(R.id.id);
            }else if(type==TYPE2){
                convertView=View.inflate(mContext,R.layout.item2,null);
                holder.image1=(ImageView)convertView.findViewById(R.id.image1);
            }

            holder.image=(ImageView)convertView.findViewById(R.id.image);
            holder.title=(TextView)convertView.findViewById(R.id.title);

            convertView.setTag(holder);
        }else{
            holder= (ViewHolder) convertView.getTag();
        }

        Data.ListBean bean = list.get(position);
        holder.title.setText(bean.getTitle());


设置特殊内容

        if(type==TYPE1){
            holder.id.setText(bean.getId()+"");
            mImageLoader.displayImage(bean.getPic(),holder.image,mOptions);
        }else if(type==TYPE2){
            String pic = bean.getPic();
            String [] urlPaths =pic.split("\\|");
            if(urlPaths.length>=2){
                mImageLoader.displayImage(urlPaths[0],holder.image,mOptions);
                mImageLoader.displayImage(urlPaths[1],holder.image1,mOptions);
            }
        }

        return convertView;
    }

    class ViewHolder{
        TextView title,id;
        ImageView image,image1;
    }
}


本文链接:https://my.lmcjl.com/post/1401.html

展开阅读全文

4 评论

留下您的评论.