1. 程式人生 > >BaseAdapter介面卡的使用

BaseAdapter介面卡的使用

public class JiuAdapter extends BaseAdapter{
    //設定list集合,上下文
    private List<String> list;
    private Context context;

    public JiuAdapter(List<String> list, Context context) {
        this.list = list;
        this.context = 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 i;
    }

    @Override
    public View getView(int i, View convertView, ViewGroup viewGroup) {
         ViewHolder viewHolder;
        if (convertView==null){
            viewHolder = new ViewHolder();
            //設定佈局
            convertView=View.inflate(context, R.layout.layout_01,null);
            //找到圖片的id
            viewHolder.img=convertView.findViewById(R.id.img_02);
            convertView.setTag(viewHolder);
        }else{
            viewHolder=(ViewHolder) convertView.getTag();
        }
        //使用Picasso把圖片設定給圖片
        Picasso.with(context).load(list.get(i)).fit().into(viewHolder.img);
        return convertView;
    }
    //建立ViewHolder 方法
    public class ViewHolder{
        ImageView img;
    }
}