在使用Glide给RecyclerView加载网络图片时,最前面两项和最后一项的图片加载失败。
接下是ImageView加载图片的代码
RecyclerView的Item的初始化:
刚开始为了能获取到当前Item的ImageView,声明了一个全局的imageView1对象;
public int getItemCount() {
return inFormationItem.getLenght();
}
class IraHolder extends RecyclerView.ViewHolder{ //第一步
private ImageView imageView;
private TextView textView;
public IraHolder(@NonNull View itemView) {
super(itemView);
imageView=itemView.findViewById(R.id.item_img);
textView=itemView.findViewById(R.id.item_name);
imageView1=imageView;
}
}
handler处理代码
设置图片时,是向imageView1设置
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
Glide.with(mContext).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1572441484977&di=a11f806b9644af04ddaf2d17800997e1&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201509%2F22%2F20150922161208_YMmn8.jpeg").into(imageView1);
break;
}
}
};
修改后的代码:
在初始化Item时,获取传入的ViewHolder,并为一个全局的ViewHolder赋值
@Override
public void onBindViewHolder(@NonNull Index_RecyclerView_Adapter.IraHolder viewHolder, final int i) {
this.viewHolder=viewHolder;
viewHolder.textView.setText(inFormationItem.getItemname(i));
viewHolder.imageView.setImageResource(R.drawable.hold);
handler.sendEmptyMessage(1);
}
handler中的代码:
private Index_RecyclerView_Adapter.IraHolder viewHolder;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
Glide.with(mContext).load("https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1572441484977&di=a11f806b9644af04ddaf2d17800997e1&imgtype=0&src=http%3A%2F%2Fb-ssl.duitang.com%2Fuploads%2Fitem%2F201509%2F22%2F20150922161208_YMmn8.jpeg").into(viewHolder.imageView);
break;
}
}
};
图片没有刷新的原因是Item没有进行重新渲染
此时可以调用notifyItemChanged(int position,Object payload);方法
position为要刷新的item,payload不能填空,这个地方可以随便填,不然,安卓会对RecyclerView进行全部的刷新。
然后在Adapter中重写
public void onBindViewHolder(@NonNull IraHolder holder, int position, @NonNull List
@Override
public void onBindViewHolder(@NonNull IraHolder holder, int position, @NonNull List
在else中对想要刷新的控件进行操作
下边贴上Adapter中全部代码
package recyclerview;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.bumptech.glide.Glide;
import com.example.time_line.R;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import GetInformation.GetInfo;
import entity.InFormationItem;
public class Index_RecyclerView_Adapter extends RecyclerView.Adapter {
private Context mContext;
private InFormationItem inFormationItem;
private Index_RecyclerView_Adapter.IraHolder viewHolder;
private int position;
Handler handler=new Handler(){
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
switch (msg.what){
case 1:
Bundle bundle=msg.getData();
int position=bundle.getInt("position");
notifyItemChanged(position,"aa");
break;
}
}
};
public Index_RecyclerView_Adapter(Context mContext, InFormationItem inFormationItem){
this.mContext=mContext;
this.inFormationItem=inFormationItem;
}
@NonNull
@Override
public Index_RecyclerView_Adapter.IraHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
return new IraHolder(LayoutInflater.from(mContext).inflate(R.layout.recyclerview_item,viewGroup,false));
}
@Override
public void onBindViewHolder(@NonNull IraHolder holder, int position, @NonNull List