package com.tjzhxx.union.adapter;

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

import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.tjzhxx.union.R;
import com.tjzhxx.union.custom_view.RoundImageView;
import com.tjzhxx.union.entity.NewBean;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 描述:
 *
 * @author yanghuaxiong
 * @create 2018/5/3 下午4:40
 */
public class NewAdapter extends RecyclerView.Adapter<NewAdapter.ViewHolder> {

    Context mContext;
    List<NewBean> mList;
    OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }

    public NewAdapter(Context mContext, List<NewBean> list) {
        this.mContext = mContext;
        this.mList = (list == null ? new ArrayList<NewBean>() : list);
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_new, parent, false);
        v.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (mOnItemClickListener != null)
                    mOnItemClickListener.onItemClick(view, (NewBean) view.getTag());
            }
        });
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        NewBean bean = mList.get(position);
        holder.itemView.setTag(bean);
        Glide.with(mContext)
                .load(bean.getPicurl())
                .placeholder(R.mipmap.bg_placeholder)
                .error(R.mipmap.bg_placeholder)
                .into(holder.image);
        holder.title.setText(bean.getDisstr());
        holder.time.setText(bean.getCreate_date());
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.image)
        RoundImageView image;
        @BindView(R.id.title)
        TextView title;
        @BindView(R.id.time)
        TextView time;


        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface OnItemClickListener {
        void onItemClick(View view, NewBean entity);
    }

}
