package com.itant.joycity.adapter;

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListAdapter;

import com.bumptech.glide.Glide;
import com.itant.joycity.R;
import com.itant.joycity.bean.NewestImage;

import java.util.List;

/**
 * Created by Jason on 2016/10/6.
 * 图片列表适配器
 */
public class NewestImageAdapter extends BaseAdapter {

    private List<NewestImage> newestImages;
    private Context context;

    public void setNewestImages(List<NewestImage> newestImages) {
        this.newestImages = newestImages;
    }

    public NewestImageAdapter() {

    }

    public NewestImageAdapter(Context context, List<NewestImage> newestImages) {
        this.newestImages = newestImages;
        this.context = context;
    }



    @Override
    public int getCount() {
        return newestImages == null ? 0 : newestImages.size();
    }

    @Override
    public Object getItem(int position) {
        return newestImages == null ? null : newestImages.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        NewestImage newestImage = newestImages.get(position);

        if (convertView == null) {
            convertView = LayoutInflater.from(context).inflate(R.layout.view_row, null);
            viewHolder = new ViewHolder();
            viewHolder.iv_header = (ImageView) convertView.findViewById(R.id.iv_header);
            viewHolder.iv_content = (ImageView) convertView.findViewById(R.id.iv_content);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Glide.with(context)
                .load(newestImage.getPreviewUrl().trim())
                .into(viewHolder.iv_header);

        Glide.with(context)
                .load(newestImage.getPicUrl().trim())
                .into(viewHolder.iv_content);

        viewHolder.iv_content.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return convertView;
    }

    private ViewHolder viewHolder;
    private static class ViewHolder {
        ImageView iv_header;
        ImageView iv_content;
    }
}
