Я загружаю удаленное изображение в строку listView, используя обратный вызов picaso. Но изображение меняется несколько раз, когда listView прокручивается очень быстро. Я предполагаю, что это связано с повторным использованием строк. Как я могу исправить это и показать правильное изображение без перезагрузки разных изображений в imageView.
Я также устанавливаю нулевое растровое изображение в ImageView. Так что он не использует предыдущее изображение при повторном использовании изображения
Вот мой код:
@Override
public View getView(int position, View convertView, ViewGroup arg2)
{
ViewHolder holder = null;
if (convertView == null)
{
LayoutInflater vi = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = vi.inflate(R.layout.row_health, arg2, false);
holder = new ViewHolder();
convertView.setTag(holder);
}
else
{
holder = (ViewHolder) convertView.getTag();
holder.imageOfDoctor_select.setImageBitmap(null);
}
loadImage("imageViewURL", relLyt_txtPicInitials, imgVw);
}
private void loadImage(String imgURL, RoundedImage imgVw, RelativeLayout relLyt_txtPicInitials){
Target target = new Target() {
@Override
public void onBitmapFailed(Drawable arg0)
{
// TODO Auto-generated method stub
}
@Override
public void onPrepareLoad(Drawable arg0)
{
// TODO Auto-generated method stub
}
@Override
public void onBitmapLoaded(Bitmap bitmap, LoadedFrom arg1)
{
if(bitmap != null){
imgVw.setVisibility(View.VISIBLE);
imgVw.setImageBitmap(bitmap);
relLyt_txtPicInitials.setVisibility(View.GONE);
}
}
};
if (!TextUtils.isEmpty(imgURL) && !imgURL.equalsIgnoreCase("null") && URLUtil.isValidUrl(imgURL))
{
Picasso.with(context).load(imgURL).into(target);
}
}
RoundedImage.java
public class RoundedImage extends ImageView
{
private float cornerRadius;
private float bitmapCornerRadius;
public RoundedImage(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
getXMLAttribute(context, attrs);
// TODO Auto-generated constructor stub
}
public RoundedImage(Context context, AttributeSet attrs)
{
super(context, attrs);
getXMLAttribute(context, attrs);
// TODO Auto-generated constructor stub
}
public RoundedImage(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
@Override
public void setImageBitmap(Bitmap bm)
{
if (bm != null)
{
bm = Utils.getRoundedCornerBitmap(bm, 50);
}
super.setImageBitmap(bm);
}
private void getXMLAttribute(Context context, AttributeSet attrs)
{
// Get proportion.
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RoundedCornerImageView);
cornerRadius = a.getDimension(R.styleable.RoundedCornerImageView_cornerRadius, 0);
a.recycle();
}
public void setCornerRadius(int radius)
{
this.cornerRadius = radius;
}
@Override
protected void onDraw(Canvas canvas)
{
boolean applyCornerToBitmap = false;
Drawable drawable = getDrawable();
if (drawable != null)
{
Bitmap bitmap = ((BitmapDrawable) drawable).getBitmap();
if (bitmap != null)
{
int viewWidth = getWidth();
if (viewWidth != 0f)
{ // Only round the corner if view width is not zero.
// Calculate the corner radius on the real bitmap, based on
// the
// corner radius of the view.
int bitmapWidth = bitmap.getWidth();
float newBitmapCornerRadius = cornerRadius * bitmapWidth / viewWidth;
// If newBitmapCornerRadius equals to bitmapCornerRadius,
// then it is not needed to set the round the corner bitmap
// to the drawable again.
if (bitmapCornerRadius != newBitmapCornerRadius)
{
applyCornerToBitmap = true;
// Create bitmap with rounded corner.
int bitmapHeight = bitmap.getHeight();
bitmapCornerRadius = newBitmapCornerRadius;
bitmap = Utils.getRoundedCornerBitmap(bitmap, 50);
// Set rounded corner bitmap to the view's drawable.
setImageBitmap(bitmap); // This will call onDraw()
// again.
}
}
}
}
// Call super onDraw() if the drawable has already been rounded.
if (!applyCornerToBitmap)
{
super.onDraw(canvas);
}
}
}