博客
关于我
Picasso 用法(旧)
阅读量:129 次
发布时间:2019-02-27

本文共 2636 字,大约阅读时间需要 8 分钟。

GRADLE

dependencies {       compile 'com.squareup.picasso:picasso:2.5.1'}

在AndroidManifest.xml中添加INTERNET权限:

给ImageView添加网络图片:

在MainActivity.java中添加

ImageView img = (ImageView)findViewById(R.id.picture);Picasso.with(this).load("https://www.baidu.com/img/bd_logo1.png?where=super").into(img);

----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

Adapter中使用Picasso

MainActivity传this,赋值mContext

MainActivity.java

MyListAdapter myListAdapter = new MyListAdapter(this,list);

ListAdapter.java

List
mList; private Context mContext; private LayoutInflater myLayoutInflater; public MyListAdapter(Context context,List
list){ mList = list; myLayoutInflater = LayoutInflater.from(context); mContext = context; }

优化

public View getView(int i, View view, ViewGroup viewGroup) {           ViewHolder holder;        ItemBean item = mList.get(i);        if(view==null){               view=myLayoutInflater.inflate(R.layout.activity_items,null);            holder= new ViewHolder();            holder.imageView = (ImageView)view.findViewById(R.id.picture);            holder.title = (TextView)view.findViewById(R.id.tv1);            holder.content = (TextView)view.findViewById(R.id.tv2);            view.setTag(holder);        }        else {               holder = (ViewHolder) view.getTag();        }        holder.title.setText(item.getItemTitle());        holder.content.setText(item.getItemContent());        //holder.imageView.setImageResource(item.getItemImageResourceId());        //改成利用Picasso框架加载图片        Picasso.with(mContext).load(item.getItemImageResourceId()).into(holder.imageView);        view.setBackgroundResource(item.getBackgroundColor());        return view;    }

ItemBean.java

public class ItemBean{       private int ItemImageResourceId;    private String ItemTitle;    private String ItemContent;    private int BackgroundColor;    public ItemBean(int itemImageResourceId, String itemTitle,String itemContent,int backgroundColor){           this.ItemImageResourceId = itemImageResourceId;        this.ItemTitle = itemTitle;        this.ItemContent = itemContent;        this.BackgroundColor = backgroundColor;    }    public String getItemTitle(){           return ItemTitle;    }    public String getItemContent(){           return ItemContent;    }    public int getItemImageResourceId(){           return ItemImageResourceId;    }    public int getBackgroundColor(){           return BackgroundColor;    }}

网站:

转载地址:http://zcob.baihongyu.com/

你可能感兴趣的文章
MYSQL一直显示正在启动
查看>>
MySQL一站到底!华为首发MySQL进阶宝典,基础+优化+源码+架构+实战五飞
查看>>
MySQL万字总结!超详细!
查看>>
Mysql下载以及安装(新手入门,超详细)
查看>>
MySQL中B+Tree索引原理
查看>>
mysql中cast() 和convert()的用法讲解
查看>>
mysql中datetime与timestamp类型有什么区别
查看>>
mysql中floor函数的作用是什么?
查看>>
MySQL中group by 与 order by 一起使用排序问题
查看>>
mysql中having的用法
查看>>
MySQL中interactive_timeout和wait_timeout的区别
查看>>
mysql中int、bigint、smallint 和 tinyint的区别、char和varchar的区别详细介绍
查看>>
mysql中json_extract的使用方法
查看>>
mysql中json_extract的使用方法
查看>>
mysql中null和空字符串的区别与问题!
查看>>
MySQL中ON DUPLICATE KEY UPDATE的介绍与使用、批量更新、存在即更新不存在则插入
查看>>
MYSQL中TINYINT的取值范围
查看>>
Mysql中varchar类型数字排序不对踩坑记录
查看>>
mysql中出现update-alternatives: 错误: 候选项路径 /etc/mysql/mysql.cnf 不存在 dpkg: 处理软件包 mysql-server-8.0的解决方法(全)
查看>>
MySQL中地理位置数据扩展geometry的使用心得
查看>>