/** * 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); } }
时间: 2024-10-11 15:51:31