我正在开发一个需要显示用户列表的应用程序。用户列表是具有imageview和textview的自定义列表。用户图像将从服务器加载到此imageview中。我已经和毕加索图书馆合作了,这很简单。我使用http请求从服务器获取图像URL和用户名。现在事实是因为我需要使用自定义列表我已经实现了自定义适配器,而在getView方法中我使用
加载图像Picasso.with(MainActivity.this).load("http://i.imgur.com/DvpvklR.png").into(container);
但我现在的问题是,当我需要在列表中显示30个用户时,我需要通过上面的代码获取30个用户图像。我不知道这是处理此类列表的最佳做法还是其他方式。我不知道这个列表可能会占用用户内存。我能做些什么?
答案 0 :(得分:1)
以下是Universal Image Loader的链接:
https://github.com/nostra13/Android-Universal-Image-Loader
它非常可定制,并带有内存缓存盒。
答案 1 :(得分:0)
您可以使用Universal Image loader库在自定义适配器中显示图像。
您可以使用此代码显示图像。
imageLoader.displayImage(imageUrl, imageView, new ImageLoadingListener()
{
@Override
public void onLoadingStarted(String imageUri, View view)
{
}
@Override
public void onLoadingFailed(String imageUri, View view, FailReason failReason)
{
}
@Override
public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage)
{
progress.setVisibility(View.GONE);
}
@Override
public void onLoadingCancelled(String imageUri, View view)
{
}
});
您可以在此处查看Android Universal Image Loader
答案 2 :(得分:0)
Picasso库易于使用并且也实现了缓存,因此不会浪费您的资源。
我在包含许多项目的列表视图中使用此库,它就像一个魅力(就像你一样)。
当项目变得可见时(当用户主要滚动时)调用getView时,不会同时下载大量图像。
我的推荐是继续使用Picasso并在列表视图适配器中实现View holder模式(您将提高性能并获得平滑滚动)。
---更新------------------------------------------- ----------------
基于this的ViewHolder模式简要示例:
“findViewById”使用循环来遍历视图中的项目,因此当您滚动列表视图时,“getView”会对其进行多次调用。它可能会导致大列表中的性能问题。
Listview重用视图项,这是因为getView接收到View参数。该参数在第一次使用时为null,但以后使用的视图可以传递给此方法。
View Holder的想法是在创建主视图对象时存储对子视图的引用,因此在将来的使用中不需要调用findViewById。
请按照以下步骤操作:
实现一个类来保存视图引用:
静态类ViewHolderItem { TextView textViewItem; //根据需要添加任意数量的项目:复选框,图像视图,... }
以这种方式在您的适配器中实现getView:
@覆盖 public View getView(int position,View convertView,ViewGroup parent){ ViewHolderItem viewHolder;
// First time: the object is created
if(convertView==null){
// inflate the layout
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(layoutResourceId, parent, false);
// Creates the ViewHolder and stores references to views
viewHolder = new ViewHolderItem();
viewHolder.textViewItem = (TextView) convertView.findViewById(R.id.textViewItem);
// Uses tag to store the holder with the main view.
convertView.setTag(viewHolder);
} else {
// View is beign resused.
// Avoids calling findViewById() just use the viewHolder.
viewHolder = (ViewHolderItem) convertView.getTag();
}
// Gets object item.
ObjectItem objectItem = getItem(position);
// assign values (if the object is not null)
if(objectItem != null) {
// Fill data in layout assigning values to views in the view holder.
viewHolder.textViewItem.setText(objectItem.itemName);
// Set images, check boxes state, or whatever you have in your layout.
}
// Finally returns the updated view.
return convertView;
}