我有一個RecyclerView來顯示列表數據,但上下滾動後,使用Volley從URL加載的圖像消失並再次出現。我知道這個清單將在閱讀How ListView's recycling mechanism works後回收物品。這是我的適配器,我錯在哪裏?滾動列表上下滾動後,Android Image在RecyclerView中消失了嗎?
public class PostAdapter extends RecyclerView.Adapter<PostAdapter.ViewHolder> {
private ArrayList<Post> posts;
private Context context;
private ImageLoader imageLoader;
public PostAdapter(ArrayList<Post> posts, Context context) {
this.posts = posts;
this.context = context;
imageLoader = MySingleton.getInstance(context).getImageLoader();
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_post, parent, false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.name.setText(posts.get(position).getName());
holder.timestamp.setText(posts.get(position).getTimestamp());
holder.status.setText(posts.get(position).getStatus());
holder.avatar.setImageUrl(Constants.URL_IMAGE + posts.get(position).getAvatar(), imageLoader);
if(!posts.get(position).getImage().equals("")) {
holder.image.setVisibility(View.VISIBLE);
holder.image.setImageUrl(Constants.URL_IMAGE + posts.get(position).getImage(), imageLoader);
} else {
holder.image.setVisibility(View.GONE);
}
}
@Override
public int getItemCount() {
return posts.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
NetworkImageView image, avatar;
TextView status, timestamp, name;
public ViewHolder(View itemView) {
super(itemView);
avatar = (NetworkImageView) itemView.findViewById(R.id.profilePic);
image = (NetworkImageView) itemView.findViewById(R.id.feedImage1);
status = (TextView) itemView.findViewById(R.id.txtStatusMsg);
name = (TextView) itemView.findViewById(R.id.name);
timestamp = (TextView) itemView.findViewById(R.id.timestamp);
}
}}
哎@tuanpit,面臨着同樣的問題,你有沒有發現修復這個?如果可以,請分享。 –
@BipinVayalu在我的情況下,因爲我使用我的電腦作爲移動應用程序的服務器,可能連接速度慢或者其他問題導致此問題。當我將我的應用程序放入真實服務器時,一切正常。對不起,因爲遲到的反應,但希望它可以幫助你和其他人 – tuanptit