after more than two weeks looking for a solution of this problem, I decided to write here. Im trying to create an app similar to that one: An app with som images and description (cardview) in a recyclerview
First at all i load all the information for my CardView from a database Title of the image, Descrition and the url from the image. When i put all that information in the RecyclerView, (title and description) shows correctly, but for the image I create a AsyncTask class to load the images from the url and let the user dont waist to much time waiting for the app to response.
If the user scroll slowly the images are loaded correctly and all is fine, but if i scroll to fast i have some problems, for example the image that is shown in the item 3 for example is shown to in the last item until the last item's image is loaded and refresh....
Sorry for my english and my explanation
Here some code for my adapter where i load the image:
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
if (holder instanceof EventosViewHolder) {
......
//Load the image (getFoto() get drawable)
if (eventoInfoAux.getFoto()==null){
CargarImagen cargarImagen = new CargarImagen(((EventosViewHolder) holder).vRelativeLayout,eventoInfo,position);
cargarImagen.execute();
}else{
((EventosViewHolder) holder).vRelativeLayout.setBackground(eventoInfoAux.getFoto());
}
}
}
Here the code for the class CargarImagen:
//Clase para cargar imagenes con una tarea asíncrona desde un url de la imagen
public class CargarImagen extends AsyncTask{
//RelativeLayout donde se introduce la imagen de fondo
RelativeLayout relativeLayout=null;
//EventoInfo para obtener la url de la imagen
List<EventoInfo> eventoInfo=null;
//Posición de la imagen clicada
int i;
//Se cargan todos los valores de las variables necesarias desde los datos introducidos
public CargarImagen(RelativeLayout relativeLayout,List<EventoInfo> eventoInfo,int i) {
this.relativeLayout = relativeLayout;
this.eventoInfo = eventoInfo;
this.i = i;
}
//Pintamos el fondo de gris mientras se está cargando la imagen
@Override
protected void onPreExecute() {
super.onPreExecute();
relativeLayout.setBackgroundResource(R.color.fondo_card_view);
}
//Se realiza la carga de la imagen en segundo plano
@SuppressWarnings("deprecation")
@Override
protected Boolean doInBackground(String... params) {
//Necesario para hacer la llamada a la red
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
//obtenemos la imagen con el metodo getBitmapFromURL
Bitmap myImage = getBitmapFromURL(eventoInfo.get(i).url);
//Si se tiene imagen se pinta, si no no se hace nada
if (myImage !=null){
Drawable dr = new BitmapDrawable(myImage);
eventoInfo.get(i).setFoto(dr);
return true;
}
return false;
}
//Al finalizar la carga de la imagen se pinta el fondo del relative layout
protected void onPostExecute(Boolean result) {
if(result){
relativeLayout.setBackground(eventoInfo.get(i).foto);
}
}
//Metodo para obtener un bitmap desde una url
public Bitmap getBitmapFromURL(String imageUrl) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
I could give you the .apk file and you could see the problem.
Thank you in advance. :)
Aucun commentaire:
Enregistrer un commentaire