RecyclerView位图不显示所有位图图像只显示最后一个,但每个项目上显示文字
以下是我在项目中使用的适配器代码:
ArrayList<Bitmap> bitmapArrayList;
PathModel pathModel=pathList.get(position);
holder.videoName= (TextView) myLayoutView.findViewById(R.id.FilePath);
holder.videoThumb= (CircularImageView)
myLayoutView.findViewById(R.id.VideoThumbnail);
holder.videoName.setText(pathModel.getPath());
holder.videoThumb.setImageBitmap(bitmapArrayList.get(position));
VideoAdapter videoAdapter=new VideoAdapter(this,pathList,bitmapArrayList);
layoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(layoutManager);
recyclerView.setAdapter(videoAdapter);
**更新的代码在我的适配器**
public class VideoAdapter extends RecyclerView.Adapter<VideoAdapter.ViewHolder>{
Context context;
ArrayList<PathModel> pathList;
View myLayoutView;
Bitmap bmThumbnail;
ArrayList<Bitmap> bitmapArrayList;
public VideoAdapter (Context context, ArrayList<PathModel> pathList, ArrayList<Bitmap> bitmapArrayList) {
this.context = context;
this.pathList=pathList;
this.bitmapArrayList=bitmapArrayList;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
myLayoutView = LayoutInflater.from(context).inflate(R.layout.video_list,
null);
return new ViewHolder(myLayoutView);
}
@Override
public void onBindViewHolder(ViewHolder myHolder,final int position) {
final ViewHolder holder = (ViewHolder ) myHolder;
PathModel pathModel=pathList.get(position);
holder.videoName= (TextView) myLayoutView.findViewById(R.id.FilePath);
holder.videoThumb= (CircularImageView)
myLayoutView.findViewById(R.id.VideoThumbnail);
holder.videoName.setText(pathModel.getPath());
holder.videoThumb.setImageBitmap(bitmapArrayList.get(position));
// holder.videoThumb.setImageDrawable(new
BitmapDrawable(context.getResources(),bitmapArrayList.get(position)));
// holder.videoThumb.setImageResource(R.drawable.ab_add_student);
//
holder.videoThumb.setImageURI(Uri.parse(pathList.get(position).getPath()));
}
@Override
public int getItemCount() {
return this.pathList.size();
}
class ViewHolder extends RecyclerView.ViewHolder {
TextView videoName;
CircularImageView videoThumb;
ViewHolder(View itemView) {
super(itemView);
}
}
}
答案 0 :(得分:1)
图像加载在android中是一项繁重的任务,因此在主线程中加载图像并不如使用Picasso或gild之类的外部库更好。
参考链接:https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en
不要忘记检查图像路径是否为空:)
答案 1 :(得分:0)
您应该在ViewHolder
内执行findViewById而不是