我正在使用从网络加载的图片填充RecyclerView
。我可以使用适配器内的AsyncTask
加载图像。但是,由于我需要与毕加索一起实施,我需要一个帮助。这是迄今为止的代码:
public class CustomAdapter extends RecyclerView.Adapter<CustomAdapter.MovieViewHolder>
{
Bitmap mBitmap;
int pos;
public static class MovieViewHolder extends RecyclerView.ViewHolder
{
CardView cv;
TextView MovieName;
ImageView MoviePhoto;
MovieViewHolder(View itemView) {
super(itemView);
cv = (CardView)itemView.findViewById(R.id.cv);
MovieName = (TextView)itemView.findViewById(R.id.movie_name);
MoviePhoto = (ImageView)itemView.findViewById(R.id.movie_photo);
}
}
List<Post> mpost;
CustomAdapter(List<Post> mpost){
this.mpost = mpost;
}
@Override
public void onAttachedToRecyclerView(RecyclerView recyclerView) {
super.onAttachedToRecyclerView(recyclerView);
}
@Override
public MovieViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.item, viewGroup, false);
MovieViewHolder pvh = new MovieViewHolder(v);
return pvh;
}
@Override
public void onBindViewHolder(MovieViewHolder movieViewHolder, int i)
{
pos=i;
movieViewHolder.MovieName.setText(mpost.get(i).getTitle());
if(mpost.get(pos).getPoster_path()!=null)
{
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL("http://image.tmdb.org/t/p/w154"+mpost.get(pos).getPoster_path());
mBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return null;
}
}.execute();
movieViewHolder.MoviePhoto.setImageBitmap(mBitmap);
}
}
@Override
public int getItemCount()
{
if(mpost!=null)
{
return mpost.size();
}
else
{
return 0;
}
}
}
我需要替换它:
new AsyncTask<Void, Void, Void>() {
@Override
protected Void doInBackground(Void... params) {
try {
URL url = new URL("http://image.tmdb.org/t/p/w154"+mpost.get(pos).getPoster_path());
mBitmap = BitmapFactory.decodeStream(url.openConnection().getInputStream());
} catch (MalformedURLException e) {
} catch (IOException e) {
}
return null;
}
}.execute();
movieViewHolder.MoviePhoto.setImageBitmap(mBitmap);
与毕加索:
Picasso.with(this)
.load("http://image.tmdb.org/t/p/w154" + mpost.get(pos).getPoster_path())
.into(MoviePhoto);
然而,似乎有错误,所以,最好的解决方案是什么?
答案 0 :(得分:3)
@Override
public void onBindViewHolder(MovieViewHolder movieViewHolder, int i){
pos=i;
movieViewHolder.MovieName.setText(mpost.get(i).getTitle());
if(mpost.get(pos).getPoster_path()!=null){
Picasso.with(getContext()).load("http://image.tmdb.org/t/p/w154" + mpost.get(pos).getPoster_path()).into(movieViewHolder.MoviePhoto)
}
}
答案 1 :(得分:0)
在方法onBindViewHolder中设置piccaso以将图像加载到图像视图
onBindViewHolder(MovieViewHolder movieViewHolder, int position)
Picasso.with(context)
.load("http://image.tmdb.org/t/p/w154" + mpost.get(position).getPoster_path())
.into(movieViewHolder.MoviePhoto);`
就这么简单。 :)