我只是做这个最近(凌晨1点实际上) - 问题是,ListView控件是积极回收的意见。因此,它获得了仍然保存位图的旧视图。
以下是你需要的东西:
[注:检查位图更早缓存。如果没有,做下面的步骤]
- 缓存位图首先尝试加载它
- 缓存目标ImageView的(和跟踪哪些位图是应该去什么位图)前。此外,清空ImageView以防万一它还包含上一个再循环项目的位图(
imageView.setImageBitmap(null);
)
- 仔细检查是否已经缓存了以前项目的缓存ImageView。如果以前已经被缓存了,那么不知怎么的说,接收到的位图不应该被加载(我通过为每个点存储一个ImageView来实现这一点,一旦接收到Bitmap,如果该行的ImageView再次为空,那么我就不会加载位图)
- 加载位图(如上所述)如果你仍然应该 - 即,如果ImageView没有再次回收。正如我前面提到的
该解决方案虽然有点复杂,但如果正确实施,效果非常好。如果你想要一个例子,请随时检查一个行动here in my current project。
编辑的要求例如:
下面是一些不那么伪伪。你仍然需要做的主要部分是实现延迟加载系统(这是一个完整的其他主题)。再次,请随时访问我上面链接的项目,特别是看看"ImageLoaderNoCache" NetworkUtil。
public class GetAllCustomerListViewAdapter extends BaseAdapter {
private JSONArray dataArray;
private Activity activity;
private static LayoutInflater inflater = null;
private static final String baseUrlForImage = "http://192.168.254.1/contact/images/";
// The list caches bitmaps as well as target imageViews, in order of the rows
private ArrayList<RowViewData> rowViewDataList;
public GetAllCustomerListViewAdapter(JSONArray jsonArray, Activity a)
{
this.dataArray = jsonArray;
this.activity = a;
inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Initialize the list
rowViewDataList = new ArrayList<RowViewData>(dataArray.length());
// This is more of a preference, but add in every item for the rowViewDataList
for(int i = 0; i < dataArray.length(); ++i) {
// Create a new, empty rowViewData instance and put it into the list
RowViewData rowData = new RowViewData();
rowViewDataList.add(rowData);
}
}
@Override
public int getCount() {
return this.dataArray.length();
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// set up convert view if it is null
final ListCell cell;
if (convertView == null)
{
convertView = inflater.inflate(R.layout.get_all_customer_list_view_cell, null);
cell = new ListCell();
cell.FullName = (TextView) convertView.findViewById(R.id.customer_full_name);
cell.Age = (TextView) convertView.findViewById(R.id.customer_age);
cell.mobile = (ImageView) convertView.findViewById(R.id.customer_mobile);
convertView.setTag(cell);
}
else
{
cell = (ListCell) convertView.getTag();
}
// change the data of cell
try
{
JSONObject jsonObject = this.dataArray.getJSONObject(position);
cell.FullName.setText(jsonObject.getString("FirstName")+" "+jsonObject.getString("LastName"));
cell.Age.setText(" "+jsonObject.getInt("Age"));
// Get the rowViewData for this position
RowViewData curRowViewData = rowViewDataList.get(position);
// If the bitmap is already cached, just use it
if(curRowViewData.bitmap != null) {
cell.mobile.setImageBitmap(curRowViewData.bitmap);
}
// Otherwise, gotta do some real work
else {
// First, set the cell's imageView's bitmap to null
// [in case this is a recycled view already with a bitmap]
cell.mobile.setImageBitmap(null);
// Then, start up the caching system
String nameOfImage = jsonObject.getString("id");
String urlForImageInServer = "http://192.168.254.1/contact/getImage.php?id="+nameOfImage;
// Create a system to load images lazily and then receive the bitmaps
// Look at my ImageLoaderNoCache for an example. Feel free to reuse it
/**
* this = this adapter should implement an interface. Look below for the corresponding method
* url = image source url
* position = position this bitmap belongs to. Will get returned along with bitmap for reference
*/
LazyImageLoader.GetBitmap(this, url, position);
// Cache/target this imageView for later
curRowViewData.targetImageView = cell.mobile;
// Double check this imageView is not being targeted already elsewhere
// ie, one of the recycling fixes [else, two bitmaps would load at once, etc]
// Also, probably could be more efficient
for(int i = 0; i < rowViewDataList.size(); ++i) {
// If this is the current position, then skip all the logic for this round
if(i == position) continue;
// Get the rowViewData for this round
// Yeah... I should've used "cell" instead of row for you...
RowViewData checkRowData = rowViewDataList.get(i);
// If the targeted ImageView is the same, null-ify it
if(checkRowData.targetImageView == curRowViewData.targetImageView) {
// The old one should be null as the bitmap should not load suddenly into
// the current recycled item
checkRowData.targetImageView = null;
// Personally, I knew that there would only be one copy at any time
// so I tried to save time by breaking out here. Feel free to be
// cautious and comment out the break statement
break;
}
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
return convertView;
}
/**
* Should be called as part of an interface with the LazyImageLoader
* @param bitmap - The lazily loaded bitmap that was requested earlier
* @param position - The row/cell position that the bitmap was requested for
*/
public void getBitmapForPosition(Bitmap bitmap, int position) {
// Get the rowViewData instance for this row/position
RowViewData curRowData = rowViewDataList.get(position);
// Cache the bitmap
curRowData.bitmap = bitmap;
// Check if the bitmap should be loaded still
if(curRowData.targetImageView != null) {
curRowData.targetImageView.setImageBitmap(bitmap);
// Not sure if strictly necessary, but good practice to make the cached view null now
curRowData.targetImageView = null;
}
}
private class ListCell
{
private TextView FullName;
private TextView Age;
private ImageView mobile;
}
// Holds all the data for lazily loading an image with a bitmap
private class RowViewData {
public ImageView targetImageView;
public Bitmap bitmap;
}
}
没有出示担保其getview()调用甚至多少次,所以如果你在这个方法中考虑asyntask它是不好的做法.. – QuokMoon 2014-12-05 18:33:16
另外,尽量在位图懒洋洋地加载和缓存。会发生什么事情是,每当你滚动到该项目上 - 无论你是否早些加载它,但滚动 - 它也会重新加载。 – MDragon00 2014-12-05 18:35:37
我不是一个母语英语的人,但不应该是“when”或“while”或“on”而不是“whem” – wawa 2014-12-05 18:35:56