我使用從web加載的圖像填充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);
然而,似乎有錯誤ddoing所以,請告訴我最好的修復?
... plz檢查我的解決方案...並讓我知道是否有任何顧慮 –
您無法使用'Picasso.with(this)'您需要將您的活動的上下文傳遞給適配器並將其用作'Picasso .with(mContext)'..你可以使用適配器構造函數傳遞活動上下文.. – Droidwala