{"id":825,"date":"2025-01-25T10:19:06","date_gmt":"2025-01-25T02:19:06","guid":{"rendered":"https:\/\/iichen.cn\/?p=825"},"modified":"2025-01-25T10:19:07","modified_gmt":"2025-01-25T02:19:07","slug":"android-listview-%e5%86%85%e5%ad%98%e3%80%81%e7%a1%ac%e7%9b%98%e7%bc%93%e5%ad%98%e5%a4%8d%e7%94%a8","status":"publish","type":"post","link":"https:\/\/iichen.cn\/?p=825","title":{"rendered":"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote is-layout-flow wp-block-quote-is-layout-flow\">\n<p>getView\u5185\u901a\u8fc7parent\u6765\u83b7\u53d6ListView\/GridView findViewWithTag\u65b9\u6cd5\u53bb\u83b7\u53d6\u5bf9\u5e94\u7684ImageView(ImageView\u8bbe\u7f6etag)<\/p>\n<\/blockquote>\n\n\n\n<pre class=\"wp-block-code language-dart lines-number has-small-font-size\"><code>public class GmGalleryGridAdapter extends BaseAdapter {\n    private Context context;\n    private List&lt;GmLocalMedia> data;\n    private final ExecutorService executorService;\n    private static final int CPU_COUNT = Runtime.getRuntime().availableProcessors(); \/\/ CPU \u6838\u5fc3\u6570\n    private static final int THREAD_POOL_SIZE = Math.min(CPU_COUNT * 2, 4); \/\/ \u7ebf\u7a0b\u6c60\u5927\u5c0f\n    private final ArrayList&lt;Future&lt;?>> activeTasks = new ArrayList&lt;>();\n    private LruCache&lt;String, Bitmap> mBitmapCache;  \/\/ \u4f7f\u7528 LruCache \u6765\u7f13\u5b58\u56fe\u7247\n\n    private OnSelectGalleryListener selectGalleryListener;\n    public GmGalleryGridAdapter(Context context, List&lt;GmLocalMedia> data,OnSelectGalleryListener onSelectGalleryListener) {\n        this.context = context;\n        this.data = data;\n        this.selectGalleryListener = onSelectGalleryListener;\n\n        \/\/ \u521d\u59cb\u5316\u7ebf\u7a0b\u6c60\n        executorService = Executors.newFixedThreadPool(THREAD_POOL_SIZE);\n\n        \/\/ \u521d\u59cb\u5316 LruCache\n        final int maxMemory = (int) (Runtime.getRuntime().maxMemory() \/ 1024); \/\/ \u83b7\u53d6\u6700\u5927\u5185\u5b58\n        final int cacheSize = maxMemory \/ 8; \/\/ \u8bbe\u7f6e\u7f13\u5b58\u5927\u5c0f\u4e3a\u6700\u5927\u5185\u5b58\u7684 1\/8\n        mBitmapCache = new LruCache&lt;String, Bitmap>(cacheSize) {\n            @Override\n            protected int sizeOf(String key, Bitmap value) {\n                return value.getByteCount() \/ 1024; \/\/ \u8fd4\u56de\u56fe\u7247\u7684\u5185\u5b58\u5360\u7528\n            }\n        };\n    }\n\n    @Override\n    public int getCount() {\n        return data.size();\n    }\n\n    @Override\n    public Object getItem(int position) {\n        return data.get(position);\n    }\n\n    @Override\n    public long getItemId(int position) {\n        return position;\n    }\n\n    private GridView mGridView;\n\n    @Override\n    public View getView(int position, View convertView, ViewGroup parent) {\n        <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-light-green-cyan-color\">if (mGridView == null) {\n            mGridView = (GridView) parent;\n        }<\/mark><\/strong>\n        GmLocalMedia media = data.get(position);\n        String path = media.getPath();\n        String fileName = media.getFileName();\n        View view;\n        ViewHolder viewHolder;\n\n        if (convertView == null) {\n            view = Ext.getLayoutView(\"gamehelper_gallery_item\");\n            viewHolder = new ViewHolder();\n            viewHolder.img = (ImageView) Ext.getWidgetView(view,\"gamehelper_gallery_item_img\");\n            view.setTag(viewHolder);\n        } else {\n            view = convertView;\n            viewHolder = (ViewHolder) view.getTag();\n        }\n        Uri uri = GmMediaUtils.isContent(path) ? Uri.parse(path) : Uri.fromFile(new File(path));\n\n        view.setOnClickListener(new View.OnClickListener() {\n            @Override\n            public void onClick(View v) {\n                Log.d(\"dqs\",\">>>>>>>>>>>>>> \u9009\u62e9\u7684\u56fe\u7247\u6587\u4ef6\u5927\u5c0f \" + media.getSize());\n                if(media.getSize() > 20 * 1024 * 1024) {\n                    Ext.toast(\"\u8bf7\u9009\u62e9\u5c0f\u4e8e20M\u7684\u56fe\u7247\", Toast.LENGTH_SHORT);\n                    return;\n                }\n                if(selectGalleryListener != null) {\n                    selectGalleryListener.onSelectGallery(uri,fileName);\n                }\n            }\n        });\n\n        <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">viewHolder.img.setTag(uri);<\/mark><\/strong>\n        viewHolder.img.setImageBitmap(BitmapFactory.decodeResource(context.getResources(), Ext.getDrawableId(\"gamehelper_image_placeholder\")));\n\n\n        \/\/ \u68c0\u67e5\u7f13\u5b58\uff0c\u82e5\u5b58\u5728\u5219\u76f4\u63a5\u52a0\u8f7d\u7f13\u5b58\u56fe\u7247\n        Bitmap bitmap = mBitmapCache.get(uri.toString());\n        if (bitmap != null) {\n            viewHolder.img.setImageBitmap(bitmap);\n        } else {\n            loadImageAsync(uri, viewHolder, position); \/\/ \u56fe\u7247\u7f13\u5b58\u4e2d\u6ca1\u6709\uff0c\u5f02\u6b65\u52a0\u8f7d\n        }\n        return view;\n    }\n\n    private Handler mHandler = new Handler(Looper.getMainLooper());\n\n\n    \/\/ \u7528\u6765\u8ddf\u8e2a\u6bcf\u4e2a\u56fe\u7247\u52a0\u8f7d\u4efb\u52a1\n\n    private void loadImageAsync(Uri uri, ViewHolder viewHolder, int position) {\n        Runnable runnable = new Runnable() {\n            @Override\n            public void run() {\n                Bitmap bitmap = loadImage(uri); \/\/ \u4ece\u78c1\u76d8\u6216\u7f51\u7edc\u52a0\u8f7d\u56fe\u7247\n                mBitmapCache.put(uri.toString(), bitmap); \/\/ \u7f13\u5b58\u56fe\u7247\n                if (bitmap != null) {\n                    \/\/ \u5728\u4e3b\u7ebf\u7a0b\u66f4\u65b0 UI\n                    mHandler.post(new Runnable() {\n                        @Override\n                        public void run() {\n                            <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-vivid-red-color\">ImageView view = mGridView.findViewWithTag(uri);<\/mark><\/strong>\n                            if (view != null) {\n                                view.setImageBitmap(bitmap);\n                            }\n                        }\n                    });\n                }\n            }\n        };\n        executorService.submit(runnable);\n    }\n\n    private Bitmap loadImage(Uri uri) {\n        try {\n            \/\/ \u5224\u65ad URI \u662f\u5426\u662f file:\/\/ \u5f00\u5934\n            if (uri != null) {\n                try {\n                    InputStream inputStream = context.getContentResolver().openInputStream(uri);\n                    BitmapFactory.Options options = new BitmapFactory.Options();\n                    options.inJustDecodeBounds = true;\n                    BitmapFactory.decodeStream(inputStream, null, options);\n\n                    \/\/ \u8ba1\u7b97\u9002\u5f53\u7684\u91c7\u6837\u7387\n                    options.inSampleSize = calculateInSampleSize(options, 80, 80); \/\/ \u6839\u636e\u9700\u6c42\u8c03\u6574\u76ee\u6807\u5bbd\u9ad8\n                    options.inJustDecodeBounds = false;\n\n                    \/\/ \u91cd\u65b0\u6253\u5f00\u8f93\u5165\u6d41\u5e76\u52a0\u8f7d\u56fe\u7247\n                    InputStream inputStreamForBitmap = context.getContentResolver().openInputStream(uri);\n                    return BitmapFactory.decodeStream(inputStreamForBitmap, null, options);\n                } catch (Exception e) {\n                    Log.d(\"dqs\", \">>>>>> \" + uri + \"&lt;>\" + e.getMessage());\n                }\n            }\n        } catch (Exception e) {\n            Log.d(\"dqs\", \"Error decoding image\", e);\n        }\n        return null;\n    }\n\n    private int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {\n        int height = options.outHeight;\n        int width = options.outWidth;\n        int inSampleSize = 1;\n\n        if (height > reqHeight || width > reqWidth) {\n            final int halfHeight = height \/ 2;\n            final int halfWidth = width \/ 2;\n            while ((halfHeight \/ inSampleSize) >= reqHeight &amp;&amp; (halfWidth \/ inSampleSize) >= reqWidth) {\n                inSampleSize *= 2;\n            }\n        }\n        return inSampleSize;\n    }\n\n    static class ViewHolder {\n        ImageView img;\n    }\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>getView\u5185\u901a\u8fc7parent\u6765\u83b7\u53d6ListView\/GridView findViewWithTag\u65b9\u6cd5\u53bb [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,8],"tags":[],"class_list":["post-825","post","type-post","status-publish","format-standard","hentry","category-android","category-8"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528 - IIchen<\/title>\n<meta name=\"description\" content=\"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/iichen.cn\/?p=825\" \/>\n<meta property=\"og:locale\" content=\"zh_CN\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528 - IIchen\" \/>\n<meta property=\"og:description\" content=\"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528\" \/>\n<meta property=\"og:url\" content=\"https:\/\/iichen.cn\/?p=825\" \/>\n<meta property=\"og:site_name\" content=\"IIchen\" \/>\n<meta property=\"article:published_time\" content=\"2025-01-25T02:19:06+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2025-01-25T02:19:07+00:00\" \/>\n<meta name=\"author\" content=\"iichen\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u4f5c\u8005\" \/>\n\t<meta name=\"twitter:data1\" content=\"iichen\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/iichen.cn\/?p=825#article\",\"isPartOf\":{\"@id\":\"https:\/\/iichen.cn\/?p=825\"},\"author\":{\"name\":\"iichen\",\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"headline\":\"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528\",\"datePublished\":\"2025-01-25T02:19:06+00:00\",\"dateModified\":\"2025-01-25T02:19:07+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/iichen.cn\/?p=825\"},\"wordCount\":9,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"articleSection\":[\"Android\",\"\u7b14\u8bb0\"],\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/iichen.cn\/?p=825#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/iichen.cn\/?p=825\",\"url\":\"https:\/\/iichen.cn\/?p=825\",\"name\":\"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528 - IIchen\",\"isPartOf\":{\"@id\":\"https:\/\/iichen.cn\/#website\"},\"datePublished\":\"2025-01-25T02:19:06+00:00\",\"dateModified\":\"2025-01-25T02:19:07+00:00\",\"description\":\"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528\",\"breadcrumb\":{\"@id\":\"https:\/\/iichen.cn\/?p=825#breadcrumb\"},\"inLanguage\":\"zh-Hans\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/iichen.cn\/?p=825\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/iichen.cn\/?p=825#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"\u9996\u9875\",\"item\":\"https:\/\/iichen.cn\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/iichen.cn\/#website\",\"url\":\"https:\/\/iichen.cn\/\",\"name\":\"IIchen\",\"description\":\"Just do it!\",\"publisher\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/iichen.cn\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"zh-Hans\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c\",\"name\":\"iichen\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"zh-Hans\",\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg\",\"contentUrl\":\"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg\",\"width\":940,\"height\":940,\"caption\":\"iichen\"},\"logo\":{\"@id\":\"https:\/\/iichen.cn\/#\/schema\/person\/image\/\"},\"sameAs\":[\"https:\/\/www.iichen.cn\"],\"url\":\"https:\/\/iichen.cn\/?author=1\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528 - IIchen","description":"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/iichen.cn\/?p=825","og_locale":"zh_CN","og_type":"article","og_title":"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528 - IIchen","og_description":"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528","og_url":"https:\/\/iichen.cn\/?p=825","og_site_name":"IIchen","article_published_time":"2025-01-25T02:19:06+00:00","article_modified_time":"2025-01-25T02:19:07+00:00","author":"iichen","twitter_card":"summary_large_image","twitter_misc":{"\u4f5c\u8005":"iichen"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/iichen.cn\/?p=825#article","isPartOf":{"@id":"https:\/\/iichen.cn\/?p=825"},"author":{"name":"iichen","@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"headline":"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528","datePublished":"2025-01-25T02:19:06+00:00","dateModified":"2025-01-25T02:19:07+00:00","mainEntityOfPage":{"@id":"https:\/\/iichen.cn\/?p=825"},"wordCount":9,"commentCount":0,"publisher":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"articleSection":["Android","\u7b14\u8bb0"],"inLanguage":"zh-Hans","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/iichen.cn\/?p=825#respond"]}]},{"@type":"WebPage","@id":"https:\/\/iichen.cn\/?p=825","url":"https:\/\/iichen.cn\/?p=825","name":"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528 - IIchen","isPartOf":{"@id":"https:\/\/iichen.cn\/#website"},"datePublished":"2025-01-25T02:19:06+00:00","dateModified":"2025-01-25T02:19:07+00:00","description":"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528","breadcrumb":{"@id":"https:\/\/iichen.cn\/?p=825#breadcrumb"},"inLanguage":"zh-Hans","potentialAction":[{"@type":"ReadAction","target":["https:\/\/iichen.cn\/?p=825"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/iichen.cn\/?p=825#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"\u9996\u9875","item":"https:\/\/iichen.cn\/"},{"@type":"ListItem","position":2,"name":"Android-ListView \u5185\u5b58\u3001\u786c\u76d8\u7f13\u5b58\u590d\u7528"}]},{"@type":"WebSite","@id":"https:\/\/iichen.cn\/#website","url":"https:\/\/iichen.cn\/","name":"IIchen","description":"Just do it!","publisher":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/iichen.cn\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"zh-Hans"},{"@type":["Person","Organization"],"@id":"https:\/\/iichen.cn\/#\/schema\/person\/4a47edf85ab49841df9e8f6aee40b77c","name":"iichen","image":{"@type":"ImageObject","inLanguage":"zh-Hans","@id":"https:\/\/iichen.cn\/#\/schema\/person\/image\/","url":"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg","contentUrl":"https:\/\/iichen.cn\/wp-content\/uploads\/2025\/01\/avatar.jpg","width":940,"height":940,"caption":"iichen"},"logo":{"@id":"https:\/\/iichen.cn\/#\/schema\/person\/image\/"},"sameAs":["https:\/\/www.iichen.cn"],"url":"https:\/\/iichen.cn\/?author=1"}]}},"_links":{"self":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/825","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=825"}],"version-history":[{"count":6,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/825\/revisions"}],"predecessor-version":[{"id":831,"href":"https:\/\/iichen.cn\/index.php?rest_route=\/wp\/v2\/posts\/825\/revisions\/831"}],"wp:attachment":[{"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=825"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=825"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/iichen.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=825"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}