package com.qhclh.ytzh.index;

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

import com.bumptech.glide.Glide;
import com.qhclh.ytzh.R;
import com.qhclh.ytzh.bean.NewsBean;

import java.util.List;

/**
 * Created by 青花瓷 on 2017/11/28.
 */
///< 首页消息
public class NewsAdapter extends BaseAdapter {

    private Context context;
    private List<NewsBean> list;
    private LayoutInflater layoutInflater;

    public NewsAdapter(Context context, List<NewsBean> list) {
        this.context = context;
        this.list = list;
        layoutInflater = LayoutInflater.from(context);
    }

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

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

    @Override
    public long getItemId(int i) {
        return list.get(i).getId();
    }

    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        ViewHolder viewHolder;
        NewsBean message = list.get(i);
        if (view == null){
            view = layoutInflater.inflate(R.layout.item_index,viewGroup,false);
            viewHolder = new ViewHolder();
            viewHolder.item_index_pic = view.findViewById(R.id.item_index_pic);
            viewHolder.item_index_title = view.findViewById(R.id.item_index_title);
            viewHolder.item_index_content = view.findViewById(R.id.item_index_content);
            viewHolder.item_index_time = view.findViewById(R.id.item_index_time);
            viewHolder.item_index_red = view.findViewById(R.id.item_index_red);

            view.setTag(viewHolder);
        }else {
            viewHolder = (ViewHolder) view.getTag();
        }

        Glide.with(context)
                .load(message.getUrl())
                .error(R.mipmap.ic_launcher_round)
                .placeholder(R.mipmap.ic_launcher_round)
                .into(viewHolder.item_index_pic);
        viewHolder.item_index_title.setText(message.getTitle());
        viewHolder.item_index_content.setText(message.getContent());
        viewHolder.item_index_time.setText(message.getTime());
        viewHolder.item_index_red.setVisibility(View.INVISIBLE);
        return view;
    }

    private class ViewHolder{
        private ImageView item_index_pic;

        private TextView item_index_title;
        private TextView item_index_content;
        private TextView item_index_time;
        private TextView item_index_red;
    }
}
