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.命名基本原则 在面向对象编程中,对于类,对象,方法,变量等方面的命名是非常有技巧的.比如,大小写的区分,使用不同字母开头等等.但究其本,追其源,在为一个资源其名称 ...
随机推荐
- Codeforces Round #277(Div 2) A、B、C、D、E题解
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud A. Calculating Function 水题,判个奇偶即可 #includ ...
- 加速ssh连接
今天aws大姨妈了,也不知道是aws问题还是gfw的问题,反正我都已经问候了你们zzsbd!!! ping aws的丢包都是75%以上,这还玩个雕啊,果断去找加速的教程来看,但是发现cygwin下并没 ...
- uWSGI
参考:http://perlmaven.com/deploying-pyton-with-uwsgi-on-ubuntu-13-10
- collections——高性能容器数据类型
由于最近对机器学习算法感兴趣,一直知道python有一个包collections封装了一些比dict,list之类高级点的类,所以抽空研究下,为接下来的工作准备. 主要参考是https://docs. ...
- python 求MD5值
(一)求字符串的MD5值 import hashlib #导入功能模块,此模块有MD5,SHA1,SHA256等方法 m = hashlib.md5() #声明一个对象 m.update(b'hell ...
- css清除浮动解决方案
清除浮动包括清除子元素的浮动和清除上级元素的浮动,其中清除上级元素的浮动,只需设置clear为both就可以了,而清除子元素的浮动则可以用空标签法.clearfix方法或overflow方法.因清除上 ...
- MVC 模型绑定
在WebForm,获取提交表单的值一般都是Request.Form["Title"]这样的方式.在MVC中,提供了模型绑定机制.让后台获取表单或Url中的参数变得更加简单. 一.基 ...
- kibana 版本kibana-4.3.1 修改地图
进入到安装目录下的src/ui/public/vislib/visualizations/目录 1.编辑_map.js文件 1 2 //url: 'https://otile{s}-s.mqcdn.c ...
- zhihu spark集群,书籍,论文
spark集群中的节点可以只处理自身独立数据库里的数据,然后汇总吗? 修改 我将spark搭建在两台机器上,其中一台既是master又是slave,另一台是slave,两台机器上均装有独立的mongo ...
- bzoj1864 [Zjoi2006]三色二叉树
Description Input 仅有一行,不超过500000个字符,表示一个二叉树序列. Output 输出文件也只有一行,包含两个数,依次表示最多和最少有多少个点能够被染成绿色. Sample ...