I am trying to use RecylerView
which Google introduced recently. I have a set of rows there, 7-8 for now, and each row has a image which I am getting from server. For this I am using Picasso library but this is not working for me. I am not sure if I am missing something or configure something.
The screen is correctly showing the default image on each row but doesn't download image from server and I wait more than 5 minutes if slow response from server but that is not the case.
Code
public DemoRecyclerAdapter(List<DemoRowModel> items, int itemLayout, final Context mContext) {
this.items = items;
this.itemLayout = itemLayout;
this.mContext = mContext;
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false);
return new ViewHolder(v);
}
@Override
public void onBindViewHolder(final ViewHolder holder, int position) {
DemoRowModel item = items.get(position);
holder.mDemoNameTextView.setText(item.getDemoName());
holder.mDemoDateTextView.setText(item.getDemoDate());
Target mTarget = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom loadedFrom) {
holder.mImageView.setImageBitmap(bitmap);
}
@Override
public void onBitmapFailed(Drawable drawable) {
Logger.d(TAG, "Failed! Bitmap could not downloaded.");
}
@Override
public void onPrepareLoad(Drawable drawable) {
}
};
Picasso.Builder builder = new Picasso.Builder(mContext);
Picasso picasso = builder.downloader(new OkHttpDownloader(mContext) {
@Override
protected HttpURLConnection openConnection(Uri uri) throws IOException {
HttpURLConnection connection = super.openConnection(uri);
// fetch the auth value
SharedPreferences mSharedPreferences = PreferenceManager.getDefaultSharedPreferences(mContext.getApplicationContext());
connection.setRequestProperty(Constant.HEADER_X_API_KEY, mSharedPreferences.getString(SharedPreferenceKeys.JSESSIONID, ""));
return connection;
}
}).build();
picasso.load(item.getImagePath()).into(mTarget);
// here set the value
holder.itemView.setTag(item);
}
Thanks in advance.
If you are using
Target
's - they should be strong referenced objects. Create fieldmTaget
in your class and moveTarget
's initialization fromonBindViewHolder
method.Edit: hold auth keys in secure place, like
keystore
. Don't save them inSharedPreferences
, it's a bad practice.Update:
1) create custom Target class
2) create custom
ImageView
3) when you initialize your imageView in viewHolder, set custom
Target
in it4) update
ViewHolder
class5) replace
ImageView
withImageViewWithTarget
in your layout6) update
onBindViewHolder
methodNow every
ImageView
will hold ownTarget
object, andTarget
isn't garbage collected.Another way: hold
Target
object inViewHolder
.