Android Adapter代码片
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Android Adapter代码片,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3421字,纯文字阅读大概需要5分钟。
内容图文
/** * 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); } }
原文:http://www.cnblogs.com/spring87/p/5538043.html
内容总结
以上是互联网集市为您收集整理的Android Adapter代码片全部内容,希望文章能够帮你解决Android Adapter代码片所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。