package com.qy.agv.activity.adapter;

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

import com.qy.agv.R;
import com.qy.agv.activity.model.MainModel;

import java.util.ArrayList;

/**
 * Created by Test on 2016/9/23.
 */
public class DirViewAdapter extends BaseAdapter {
    // 填充数据的list
    private ArrayList<MainModel> list;
    // 用来导入布局
    private LayoutInflater inflater = null;
    private Context context;
    private View.OnTouchListener onTouchListener;

    // 构造器
    public DirViewAdapter(ArrayList<MainModel> list, Context context) {
        this.list = list;
       this.inflater =  LayoutInflater.from(context);;
        this.context = context;
    }

    public void setOnTouchListener(View.OnTouchListener onTouchListener) {
        this.onTouchListener = onTouchListener;
    }

    @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;
    }

    public int getDrawableIdByName( String name) {
        return context.getResources().getIdentifier(name, "mipmap", context.getPackageName());
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        MainHolder holder = new MainHolder();
        MainModel shop = list.get(position);
        if (convertView == null) {
            convertView = inflater.inflate(R.layout.qy_home_item, null);
            holder.logo = (ImageView) convertView.findViewById(R.id.logo);
            holder.shopTitle = (TextView)convertView.findViewById(R.id.title);
            convertView.setTag(holder);
        } else {
            holder = (MainHolder) convertView.getTag();
        }

        if (shop.getLogo()!=null){
            holder.logo.setImageResource(getDrawableIdByName(shop.getLogo()));
        }else{
            holder.logo.setVisibility(View.INVISIBLE);
            holder.shopTitle.setVisibility(View.INVISIBLE);
        }
        if(shop.getTitle()!=null){
            holder.shopTitle.setText(shop.getTitle());
        }
        convertView.setId(position);
        convertView.setOnTouchListener(onTouchListener);
        return convertView;
    }

    static class MainHolder {
        public ImageView logo;
        public TextView shopTitle;
    }
}
