我正在使用Glide库从URL加载图片,这些URL来自Graph Request (Facebook)。它在RecyclerAdapter中使用。当我滚动时,每个ImageView都会在不到一秒的时间内显示错误的图片,然后才会显示正确的图片。以下是代码:
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder holder, final int position) {
ImageView main_image = viewHolder.main_image,
getEventCover(position, main_image);
}
private void getEventCover(int position, final ImageView img){
GraphRequest request = GraphRequest.newGraphPathRequest(
AccessToken.getCurrentAccessToken(),
event.get(position).getEventID(),
new GraphRequest.Callback() {
@Override
public void onCompleted(GraphResponse response) {
JSONObject data = response.getJSONObject();
try {
JSONObject cover = data.getJSONObject("cover");
String source = cover.getString("source");
Glide.with(fragment)
.load(source)
.diskCacheStrategy(DiskCacheStrategy.ALL)
.centerCrop()
.into(img);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
);
Bundle parameters = new Bundle();
parameters.putString("fields", "cover");
request.setParameters(parameters);
request.executeAsync();
}