• 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);
            }
        }
    

      

  • 相关阅读:
    Java实现 LeetCode 365 水壶问题
    Java实现 LeetCode 363 矩形区域不超过 K 的最大数值和
    Java实现 LeetCode 363 矩形区域不超过 K 的最大数值和
    Java实现 LeetCode 363 矩形区域不超过 K 的最大数值和
    Java实现 LeetCode 357 计算各个位数不同的数字个数
    Java实现 LeetCode 357 计算各个位数不同的数字个数
    Java实现 LeetCode 357 计算各个位数不同的数字个数
    Java实现 LeetCode 355 设计推特
    利用linux信号机制调试段错误(Segment fault)
    LIBRARY_PATH和LD_LIBRARY_PATH环境变量的区别
  • 原文地址:https://www.cnblogs.com/spring87/p/5538043.html
Copyright © 2020-2023  润新知