package com.zztrapp.zztrapp.ui.index.adapter;

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


import java.util.List;

/**
 * 该适配器通用与ListView与Gridview
 * @param <T>
 */

public abstract class CommonAdapter<T> extends BaseAdapter {
    protected LayoutInflater mInflater;
    protected Context mContext;
    protected List<T> mDatas;
    protected final int mItemLayoutId;

    public CommonAdapter(Context context, int itemLayoutId) {
        this.mContext = context;
        this.mInflater = LayoutInflater.from(mContext);
        this.mItemLayoutId = itemLayoutId;
    }

    public void setData(List<T> mDatas) {
        this.mDatas = mDatas;
        notifyDataSetChanged();
    }

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

    @Override
    public T getItem(int position) {
        return mDatas != null ? mDatas.get(position) : null;
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final ViewHolder viewHolder = getViewHolder(position, convertView, parent);

        convert(viewHolder, getItem(position));
        convert(viewHolder, getItem(position),position);
        convert(viewHolder, getItem(position),convertView);
        convert(viewHolder, getItem(position),position,convertView);
        return viewHolder.getConvertView();

    }

    public  void convert(ViewHolder helper, T item, int position, View convertView){};
    public  void convert(ViewHolder helper, T item){};
    public  void convert(ViewHolder helper, T item,int position){};
    public  void convert(ViewHolder helper, T item,View convertView){};

    private ViewHolder getViewHolder(int position, View convertView,
                                     ViewGroup parent) {
        return ViewHolder.get(mContext, convertView, parent, mItemLayoutId,
                position);
    }

}  