package cn.edu.xjau.recyclerviewtest;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

/**
 * Created by lp on 2018/3/28.
 */

public  class CatAdapter extends RecyclerView.Adapter<CatAdapter.ViewHolder> {
    private List<Cat> mCatList;
	 static class ViewHolder extends RecyclerView.ViewHolder{
        ImageView catImage;
        TextView catName;

        public ViewHolder(View view){
            super(view);
            catImage = (ImageView) view.findViewById(R.id.cat_image);
            catName = (TextView) view.findViewById(R.id.cat_name);
        }
    }

    public CatAdapter(List<Cat> mCatList) {
        this.mCatList = mCatList;
    }
	
	//创建ViewHolder实例
    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.cat_item,parent,false);
        ViewHolder holder = new ViewHolder(view);

        return holder;
    }

	//当recyclerView滚动到屏幕内时
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        Cat cat = mCatList.get(position);
        holder.catImage.setImageResource(cat.getImageId());
        holder.catName.setText(cat.getName());

    }

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

   

}
