Android Adapter代码片
/**
* Adapter for grid of coupons.
*/
private static class CouponAdapter extends BaseAdapter { private LayoutInflater mInflater;
private List<Coupon> mAllCoupons; /**
* Constructs a new {@link CouponAdapter}.
*
* @param inflater to create new views
* @param allCoupons for list of all coupons to be displayed
*/
public CouponAdapter(LayoutInflater inflater, List<Coupon> allCoupons) {
if (allCoupons == null) {
throw new IllegalStateException("Can't have null list of coupons");
}
mAllCoupons = allCoupons;
mInflater = inflater;
} @Override
public int getCount() {
return mAllCoupons.size();
} @Override
public Coupon getItem(int position) {
return mAllCoupons.get(position);
} @Override
public long getItemId(int position) {
return position;
} @Override
public View getView(int position, View convertView, ViewGroup parent) { //缓存策略的另外一种写法 View result = convertView;
if (result == null) {
//注意mInflater的来源,是在Activity的setContextView中这样写的:
// Fetch the {@link LayoutInflater} service so that new views can be created
// LayoutInflater inflater = (LayoutInflater) getSystemService(
// Context.LAYOUT_INFLATER_SERVICE);
result = mInflater.inflate(R.layout.grid_item, parent, false);
} // Try to get view cache or create a new one if needed
ViewCache viewCache = (ViewCache) result.getTag();
if (viewCache == null) {
viewCache = new ViewCache(result);
result.setTag(viewCache);
} // Fetch item
Coupon coupon = getItem(position); // Bind the data
viewCache.mTitleView.setText(coupon.mTitle);
viewCache.mSubtitleView.setText(coupon.mSubtitle);
viewCache.mImageView.setImageURI(coupon.mImageUri); return result;
}
} /**
* Cache of views in the grid item view to make recycling of views quicker. This avoids
* additional {@link View#findViewById(int)} calls after the {@link ViewCache} is first
* created for a view. See
* {@link CouponAdapter#getView(int position, View convertView, ViewGroup parent)}.
*/
private static class ViewCache { /** View that displays the title of the coupon */
private final TextView mTitleView; /** View that displays the subtitle of the coupon */
private final TextView mSubtitleView; /** View that displays the image associated with the coupon */
private final ImageView mImageView; /**
* Constructs a new {@link ViewCache}.
*
* @param view which contains children views that should be cached.
*/
private ViewCache(View view) {
mTitleView = (TextView) view.findViewById(R.id.title);
mSubtitleView = (TextView) view.findViewById(R.id.subtitle);
mImageView = (ImageView) view.findViewById(R.id.image);
}
} /**
* 关于适配器里面数据bean对象问题,如果只是纯粹展示,而不需要改变bean对象的属性,那么推荐下面这种方式,如果需要改变
* bean对象的属性,那么还是用常见的get set方法实现.
*/
private static class Coupon { /** Title of the coupon. */
private final String mTitle; /** Description of the coupon. */
private final String mSubtitle; /** Content URI of the image for the coupon. */
private final Uri mImageUri; /**
* Constructs a new {@link Coupon}.
*
* @param titleString is the title
* @param subtitleString is the description
* @param imageAssetFilePath is the file path from the application's assets folder for
* the image associated with this coupon
*/
private Coupon(String titleString, String subtitleString, String imageAssetFilePath) {
mTitle = titleString;
mSubtitle = subtitleString;
mImageUri = Uri.parse("content://" + AssetProvider.CONTENT_URI + "/" +
imageAssetFilePath);
}
}
Android Adapter代码片的更多相关文章
- Android进阶(十四)Android Adapter详解
Android Adapter详解 Android是完全遵循MVC模式设计的框架,Activity是Controller,layout是View.因为layout五花八门,很多数据都不能直接绑定上去, ...
- [转]Android Adapter以及getView()方法的理解
Android Adapter基本理解: 我的理解是: 1.一个有许多getter的类(就是getView(),getCount()....这些方法) 2.有多少个get方法?都是什么? 这些gett ...
- 孟老板 ListAdapter封装, 告别Adapter代码 (上)
BaseAdapter封装(一) 简单封装 BaseAdapter封装(二) Header,footer BaseAdapter封装(三) 空数据占位图 BaseAdapter封装(四) PageHe ...
- 孟老板 ListAdapter封装, 告别Adapter代码 (三)
BaseAdapter系列 ListAdapter封装, 告别Adapter代码 (一) ListAdapter封装, 告别Adapter代码 (二) ListAdapter封装, 告别Adapter ...
- Android Adapter基本理解
感谢大佬:https://blog.csdn.net/l799069596/article/details/47301711 Android Adapter基本理解: 我的理解是: 1.一个有许多ge ...
- Intellij idea 和android studio 代码给混淆
Intellij idea 和android studio 代码给混淆 一.指令说明-optimizationpasses 5 # 指定代码的压缩级别 -dontusemixedcaseclassna ...
- Android实用代码七段(五)
前言 每次分享意味着每次都有进步,本系列以实用为主,欢迎和我分享和推荐好用的代码段~~ 声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯 ...
- Android实用代码七段(四)
声明 欢迎转载,但请保留文章原始出处:) 博客园:http://www.cnblogs.com 农民伯伯: http://over140.cnblogs.com 正文 1.发送不重复的通知(Notif ...
- Android开发代码规范(转)
Android开发代码规范 1.命名基本原则 在面向对象编程中,对于类,对象,方法,变量等方面的命名是非常有技巧的.比如,大小写的区分,使用不同字母开头等等.但究其本,追其源,在为一个资源其名称 ...
随机推荐
- Python初学
经同学推荐,学习了下Python语言,看Python的介绍,它本身是一个面向对象的解释型脚本语言,我初看到这句话的时候就在想,一个脚本语言还搞成面向对象?有这个必要么?原谅我肤浅了一把. 它还被俗称为 ...
- Ubuntu13.04手动安装nvidia显卡驱动
1. 下载最新版的nVidia驱动,命名为NVIDIA.run. http://www.nvidia.com/page/drivers.html 2.编辑blacklist.conf. sudo ge ...
- 【2】开发环境的搭建,Ubuntu14.04
这里使用的是Ubuntu14.04 Unity 更新源 首先,将更新源更换为国内更新源,我这里使用的是网易的更新源 sudo gedit /etc/apt/sources.list deb http: ...
- Histats申请Counter网站计数器 - Blog透视镜
为了计算网页被浏览的次数,访客人数等统计数据,作为未来分析之用,可以向Histats申请免费的Counter网站计数器,它的功能相当齐全,同时也会保留一段时间的资料,当作统计比较的资料,更可以进一步付 ...
- PV与并发之间换算的算法 换算公式
- App 推荐:Spotify
之前一直用网易云音乐听歌,后来因为沸沸扬扬的 XCode 注入病毒事件,就把它卸了(无奈脸=.=) 对比国内其它音乐App,私心觉得网易云音乐还是不错的.没有很多花里胡哨的装饰,里面的歌单做的也还算精 ...
- Multiple outputs from T4 made easy – revisited » DamienG
Multiple outputs from T4 made easy – revisited » DamienG Multiple outputs from T4 made easy – revisi ...
- 谷歌Cartographer学习(1)-快速安装测试(转载)
转载自谷歌Cartographer学习(1)-快速安装测试 代码放到个人github上,https://github.com/hitcm/ 如下,需要安装3个软件包,ceres solver.cart ...
- crm使用url打开窗口视图
//URL可寻址元素使您能够包含指向Microsoft Dynamics CRM窗口. 视图. 对话框和其它应用程序中的报告. //这样.您就能够轻松扩展其它应用程序.报表或站点,以便用户无需切换应用 ...
- C# Socket 简易的图片传输
关于网络的数据传输我就是个小白,所以今天学习一下简易的Socket图片传输. 客户端和服务器的连接咱们上次已经学过了,咱们先从简易的文件传输入手.下面开始代码分析了. Server.cs using ...