每次滚动视图时,Android都会将url中的图像加载到listViiew中

时间:2016-05-08 14:03:35

标签: android listview

我有一个列表视图,每行都是关于文本和从网址加载的图片。

在第一次加载View时,前三行将被渲染并填充文本,并在几秒钟内将图像加载到另一个线程中。

当我向下滚动时,新行也显示没有图像并加载它,但当我向上滚动时,我发现之前上传的图像已经消失,文本只存在并重新加载图像。

我想一次加载图片,当我上下滚动时,图片一旦加载就不再重新加载。

这是我的代码:

 public class MainActivity extends AppCompatActivity {
        ListViewCountries listViewCountries =new ListViewCountries();
        @Override
         protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                listViewCountries.FillCountriesList();


  setContentView(R.layout.activity_main);
            CountryAdapter adapter=new CountryAdapter();
            ListView lst=(ListView)findViewById(R.id.listView);
            lst.setAdapter(adapter);
        }
        class  CountryAdapter extends BaseAdapter {

            @Override
            public int getCount() {
                return listViewCountries.Count;
            }

            @Override
            public Object getItem(int position) {
                return listViewCountries.Countries.get(position);
            }

            @Override
            public long getItemId(int position) {
                return position;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                    List<Country> items = listViewCountries.Countries;
                    LayoutInflater inflater = getLayoutInflater();
                    View rowView = inflater.inflate(R.layout.row_cell, null);
                    ImageView img = (ImageView) rowView.findViewById(R.id.flagImg);
                    new DownloadImageTask(img).execute(items.get(position).FlagURL);
                    TextView name = (TextView) rowView.findViewById(R.id.countryName);
                    TextView description = (TextView) rowView.findViewById(R.id.shortText);
                    name.setText(items.get(position).Name);
                    description.setText(items.get(position).ShortDescription);
                    return rowView;


            }
        }
        private class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
            ImageView bmImage;

            public DownloadImageTask(ImageView bmImage) {
                this.bmImage = bmImage;
            }

            protected Bitmap doInBackground(String... urls) {

                String urldisplay = urls[0];
                Bitmap mIcon11 = null;
                try {
                    InputStream in = new java.net.URL(urldisplay).openStream();
                    mIcon11 = BitmapFactory.decodeStream(in);
                } catch (Exception e) {
                    Log.e("Error", e.getMessage());
                    e.printStackTrace();
                }
                return mIcon11;
            }

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

1 个答案:

答案 0 :(得分:2)

从您的代码中可以看出这一点。在getView中你正在调用

$text = str_replace(array("'",'"'), array('’'), $text);    
$text = htmlentities(str_replace(array('"', "'"), '’', $text);
$text = htmlentities(str_replace(array('"', "'"), '’', $_POST['text']));
$text = str_replace("'" ,"&rsquo;",$text);
$text = str_replace("'" ,"’",$text);
$text = str_replace(array("'"), "&rsquo;", $text);
$text = str_replace(array("\'", "'", "&quot;"), "&rsquo;", htmlspecialchars($text));
$text = str_replace(array('\'', '"'), '’', $text);
$text = str_replace(chr(39), chr(146), $text);
$text  = str_replace("'", "&ampquot;", $text); 

滚动时,为每个项目调用getView,因此会再次加载图像,因为在getView中从url获取图像,没有缓存机制。您需要做的是缓存图像的副本,这样就不会一次又一次地下载图像,只是显示缓存中存在的文件。您可以尝试手动构建缓存,例如this

或者更好的是你可以使用picasso。它很容易使用,只需按照文档。