安卓:如何优化的ImageView + 3 TextViews的ListView?ImageView、TextViews、ListView

2023-09-07 09:36:51 作者:够没

我一直在到处找,看看是否有实现这一目标的标准方法,但我觉得不同的解决方案的每个时间。

I've been looking everywhere to see if there is a standard way of achieving this but I find a different solution every-time.

基本上,我试图建立一个自定义的ListView 用图像和文字两三条线除了它。

Basically, I am trying to build a Custom ListView with an image and two-three lines of text besides it.

为了优化它,我明白,下面已经被使用:

In order to optimize it, I understand that the following have to be used:

convertView :基本上,如果认为已经膨胀,用它

convertView: Basically if the view was already inflated, use it

延迟加载:直到他们被要求不要加载行的内容

Lazy-Loading: Do not load the content of the rows until they are called for

背景下载和放大器;缓存:在自己的线程下载图像,然后更新该行(?和可能的缓存它们)

Background Downloading & Caching: Download images in their own threads and then update the row (and possible cache them?)

我可以管理1和2,但第三个是真正困惑我。

I could manage 1 and 2 but the third one is really confusing me.

有没有最佳实践呢?

感谢

推荐答案

感谢马克对他的帮助。这是在做什么,他建议的一种方式(以防万一还有一些好奇):

Thanks to Mark for his help. This is one way of doing what he suggested (just in case some else is curious):

private class DownloadImageTask extends AsyncTask<Object, Integer, Bitmap> {

    		private ImageView iv;

    		protected Bitmap doInBackground(Object... params) {
    			try {
    				iv = (ImageView) params[0];
    	            URL aURL = new URL("https://m.xsw88.com/allimgs/daicuo/20230907/4908.png" ); 
    	            URLConnection conn = aURL.openConnection(); 
    	            conn.connect(); 
    	            InputStream is = conn.getInputStream(); 
    	            BufferedInputStream bis = new BufferedInputStream(is); 
    	            Bitmap bm = BitmapFactory.decodeStream(bis); 
    	            bis.close(); 
    	            is.close();
    	            return bm;
    	       } catch (IOException e) { 
    	            return null;
    	       } 
    		}

    		protected void onPostExecute(Bitmap result) {
    			iv.setImageBitmap((Bitmap) result);
    		}
    	}

和如下它将被使用:

new DownloadImageTask().execute(new Object[] {ImageViewHandle, position});

请注意,这不是一个工作code。这是从一个较大的code底部取出,所以你将不得不做出适当的修改,以使这项工作。

Note that this is not a working code. This was taken from a larger code base so you will have to make appropriate changes to make this work.