I am using the recyclerview to list the image without using third party.I fetch the image from internet.I face the problem that before loading the image on the particular position,i got other image on the particular position for some times after that i got a original image while scrolling.
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
final ImageDetail current=data.get(position);
// holder.image.setImageResource(current.imageid);.
Drawable place=holder.image.getContext().getResources().getDrawable(R.drawable.ic_place);
holder.image.setImageDrawable(place);
String url=current.imageuri;
// notifyItemInserted(position);
new ShowImage(holder.image).execute(url);
}
enter code here */
public class ShowImage extends AsyncTask<String,Void,Bitmap>{
private WeakReference<ImageView> imageview;
public ShowImage(ImageView imv){
imageview=new WeakReference<ImageView>(imv);
}
/* Background process
* input:url
* output: Bitmap image
* It passed into onPostExecute method
*
*/
@Override
protected Bitmap doInBackground(String... urls) {
return getBitMapFromUrl(urls[0]);
}
/* This method called after the doINputBackground method
* input:Bitmap image
* output: image set into the image view
* Image view passed from RecyclerViewOperation to ShowImage class through constructor
*
*/
@Override
protected void onPostExecute(Bitmap result) {
if((imageview!=null)&&(result!=null)){
ImageView imgview=imageview.get();
if(imgview!=null){
imgview.setImageBitmap(result);
}
}
}
/* This method called by doInBackground method
* input:url
* output: Bitmap image
*
*/
private Bitmap getBitMapFromUrl( String imageuri){
HttpURLConnection connection=null;
try {
URL url=new URL(imageuri);
connection= (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream is=connection.getInputStream();
Bitmap mybitmap=BitmapFactory.decodeStream(is);
return mybitmap;
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
finally {
if(connection!=null) {
connection.disconnect();
}
}
}
}