Android Adapter代碼片

來源:互聯網
上載者:User

標籤:

 /**     * 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代碼片

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.