Bru*_*uno 4 android image android-asynctask android-cardview android-recyclerview
我正在尝试创建类似于该应用程序的应用程序: 一个在recyclerview中具有一些图像和描述(卡片视图)的应用程序
首先,我从图像的数据库标题,描述和图像的URL加载CardView的所有信息。当我将所有信息放到RecyclerView中时,(标题和描述)可以正确显示,但是对于图像,我创建了一个AsyncTask类来从URL加载图像,从而使用户不必等待太多时间来等待应用程序响应。
如果用户缓慢滚动,图像加载正确并且一切都很好,但是如果我快速滚动,则会遇到一些问题,例如,项目3中显示的图像显示在最后一个项目中,直到最后一个项目为止图像已加载并刷新。
这是我的适配器在其中加载图像的一些代码:
@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());
}
}
}
Run Code Online (Sandbox Code Playgroud)
这里是CargarImagen类的代码:
//Clase para cargar imagenes con una tarea asíncrona desde un url de la imagen
public class CargarImagen extends AsyncTask<String, String, Boolean>{
//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;
}
}
}
Run Code Online (Sandbox Code Playgroud)
我可以给您.apk文件,您可以看到问题。
先感谢您。:)
这是我的适配器的完整代码
import java.util.List;
import com.abdevelopers.navarraongoing.R;
import com.abdevelopers.navarraongoing.detalle.DetalleActivity;
import android.content.Intent;
import android.support.v7.widget.CardView;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class EventosAdapter extends RecyclerView.Adapter<ViewHolder>{
private static final int TYPE_EVENTO = 0;
private static final int TYPE_FOOTER = 1;
//lista de eventos
private List<EventoInfo> eventoInfo;
private String usuario;
private EventoInfo eventoInfoAux;
private PaginaInicioActivity paginaInicio;
//Se pasan los valores necesarios para obtener información de los eventos
public EventosAdapter(List<EventoInfo> eventoInfo, String usuario, PaginaInicioActivity paginaInicio ) {
this.eventoInfo = eventoInfo;
this.usuario = usuario;
this.paginaInicio = paginaInicio;
}
//Metodo para obtener el numero de items en la lista que introducimos
@Override
public int getItemCount() {
return eventoInfo.size();
}
@Override
public int getItemViewType(int position) {
if (position + 1 == getItemCount()) {
return TYPE_FOOTER;
} else {
return TYPE_EVENTO;
}
}
//Se asignan los datos a cada uno de los elementos de la cardview
@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
if (holder instanceof EventosViewHolder) {
//Obtenemos cada uno de los eventos
eventoInfoAux = eventoInfo.get(position);
//eventosViewHolder = holder;
//Introducimos el título del evento
((EventosViewHolder) holder).vTitle.setText(eventoInfoAux.titulo);
//Introdicumos la fecha y el lugar del evento
((EventosViewHolder) holder).vFechaLugar.setText(eventoInfoAux.fecha+", "+eventoInfoAux.lugar);
//obtenemos el numero de asistentes al evento
String asistentes = Integer.toString(eventoInfoAux.asistentes);
((EventosViewHolder) holder).vLikeButton.setText(asistentes);
//Se pinta el boton de like dependiendo de si está o no like
if(eventoInfoAux.like){
((EventosViewHolder) holder).vLikeButton.setBackgroundResource(R.drawable.button_like_liked);
}else{
((EventosViewHolder) holder).vLikeButton.setBackgroundResource(R.drawable.button_like);
}
//Se pinta la imagen del evento dependiendo de si está en la base de datos o no
if (eventoInfoAux.foto==null){
CargarImagen cargarImagen = new CargarImagen(((EventosViewHolder) holder).vRelativeLayout,eventoInfo,position);
cargarImagen.execute();
}else{
((EventosViewHolder) holder).vRelativeLayout.setBackground(eventoInfoAux.foto);
}
}
}
@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
if (viewType == TYPE_EVENTO) {
View view = LayoutInflater.
from(parent.getContext()).
inflate(R.layout.evento_card_view, parent, false);
return new EventosViewHolder(view,eventoInfo,usuario,paginaInicio);
} else if (viewType == TYPE_FOOTER) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.pie_carga_pagina_inicio, parent, false);
view.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
return new FooterViewHolder(view);
}
return null;
}
//Clase para crear el footerview donde se cargan más eventos
class FooterViewHolder extends ViewHolder {
public FooterViewHolder(View view) {
super(view);
}
}
//Clase para crear los eventos con su cardview
class EventosViewHolder extends ViewHolder implements View.OnClickListener{
private TextView vTitle;
private TextView vFechaLugar;
private Button vLikeButton;
private RelativeLayout vRelativeLayout;
private List<EventoInfo> eventoInfo;
private String usuario;
private LikesDDBB likesManager;
private PaginaInicioActivity paginaInicio;
private CardView vCardView;
public EventosViewHolder(View itemView,List<EventoInfo> eventoInfo,String usuario,
PaginaInicioActivity paginaInicio) {
super(itemView);
this.paginaInicio = paginaInicio;
this.usuario = usuario;
vTitle = (TextView)itemView.findViewById(R.id.titulo);
vFechaLugar = (TextView) itemView.findViewById(R.id.fecha_lugar);
vLikeButton = (Button) itemView.findViewById(R.id.like_button);
vRelativeLayout = (RelativeLayout) itemView.findViewById(R.id.layout_cardview);
vCardView = (CardView)itemView.findViewById(R.id.card_view);
//Valores por defecto de los botones y del fondo en caso de no haber like ni foto
vLikeButton.setBackgroundResource(R.drawable.button_like);
vRelativeLayout.setBackgroundResource(R.color.fondo_card_view);
vLikeButton.setOnClickListener(this);
vCardView.setOnClickListener(this);
vLikeButton.setTag("boton");
vCardView.setTag("evento");
this.eventoInfo = eventoInfo;
}
@SuppressWarnings("deprecation")
@Override
public void onClick(View v) {
if(v.getTag().equals("boton")){
likesManager = new LikesDDBB(this.eventoInfo.get(getPosition()).like, usuario,
vLikeButton, getPosition(), eventoInfo, paginaInicio);
likesManager.execute();
}else if(v.getTag().equals("evento")){
Intent inicion = new Intent(paginaInicio,DetalleActivity.class);
paginaInicio.startActivity(inicion);
}
}
}
//Para obtener la lista de eventos desde la clase PaginaInicioActivity
public List<EventoInfo> getEventoInfo() {
return eventoInfo;
}
public void setEventoInfo(List<EventoInfo> eventoInfo) {
this.eventoInfo = eventoInfo;
}
}
Run Code Online (Sandbox Code Playgroud)
正如名称“ RecyclerView”所指示的,它回收/重复使用创建的视图来显示您的物品/卡片。
因此,假设您的RecyclerView具有3个CardViews,您可以在滚动时对其进行回收,并且我们有4个项目来显示其内容。
最初,第1项的内容显示在CardView 1中,第2项显示在CardView 2中,而第3项显示在CardView 3中。
现在,随着滚动,CardView 1消失,被回收,并且CardView 1中显示项目4的内容。
只要您不重置之前插入的内容,CardView 1就会显示它们-在您的情况下-只要AsyncTask需要完成之前设置项目的内容才会显示。
为了解决您的问题,您可能需要使用图像加载(和缓存)库,例如:
您的解决方案也容易出现竞争状况(当较晚的任务在较早的任务之前完成时)