Load images in listview

Asked

Viewed 1,292 times

0

I have a listview with this layout

layoutlist.xml

<?xml version="1.0" encoding="utf-8"?>

<TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Praia riviera são lourenço"
    android:background="@drawable/fundop"
    android:textColor="#000000"
    android:gravity="center"
    android:id="@+id/nomePraia"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" />

<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/nomePraia"
    android:layout_above="@+id/bt"
    android:layout_marginBottom="10dp"
    android:layout_marginTop="10dp">

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:id="@+id/fotoPraia"

        android:layout_centerHorizontal="true" />

    <ProgressBar
        android:indeterminate="true"
        android:id="@+id/progress"
        style="?android:attr/progressBarStyle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceSmall"
    android:text="Coemçar a descrição ...."
    android:id="@+id/descricao"
    android:maxLength="400"
    android:textColor="#000000"
    android:layout_alignTop="@+id/btFavorito"
    android:layout_toEndOf="@+id/btFavorito"
    android:gravity="left"
    android:layout_toStartOf="@+id/bt"
    android:layout_toRightOf="@+id/btFavorito"
    android:layout_toLeftOf="@+id/bt"
    android:layout_marginBottom="10dp"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp"/>


<RelativeLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_marginBottom="10dp">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btFavorito"
        android:layout_alignParentBottom="true"
        android:background="@drawable/ic_action_favorite"
       android:layout_marginLeft="7dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />
    <Button
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/btVerMais"
        android:id="@+id/btVerMais"
        android:background="#fff"
        android:textColor="@color/wallet_link_text_light"
        android:layout_alignTop="@+id/bt"
        android:layout_centerHorizontal="true" />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/bt"
        android:background="@drawable/ic_action_overflow"

        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />
</RelativeLayout>

Adapter.java

View view;
    view = convertView;
    final ViewHolder holder;
    if(convertView == null) {
        view = mInflater.inflate(R.layout.layoutlist, parent, false);
        holder = new ViewHolder();


        holder.foto = (ImageView) view.findViewById(R.id.fotoPraia);
        holder.btFavorito = (ImageButton) view.findViewById(R.id.btFavorito);
        holder.nomePraia = (TextView) view.findViewById(R.id.nomePraia);
        holder.descricao = (TextView) view.findViewById(R.id.descricao);
        holder.bt = (ImageButton) view.findViewById(R.id.bt);
        holder.btVerMais = (Button) view.findViewById(R.id.btVerMais);
        holder.pb = (ProgressBar) view.findViewById(R.id.progress);
        holder.position=position;
        view.setTag(holder);

        bitmapTask = new LoadBitmapImage(view,holder.foto,width,context);
    }else {
        view = convertView;
        holder = (ViewHolder)view.getTag();
        bitmapTask = new LoadBitmapImage(view,holder.foto,width,context);
    }

    holder.pb.setVisibility(View.VISIBLE);
    holder.foto.setImageBitmap(fundoBranco);
    final ListenerItem categoria = itens.get(position);
    final int drawableId = context.getResources().getIdentifier(categoria.nome_foto, "drawable", packageName);

    bitmapTask.execute(drawableId);

    String desc="";
    if(categoria.descricao.length()<397){
        holder.btVerMais.setVisibility(View.INVISIBLE);
        desc=categoria.descricao;
    }else{
        holder.btVerMais.setVisibility(View.VISIBLE);
        for(int i=0;i<397;i++){
            desc=desc+categoria.descricao.charAt(i);
        }
        desc=desc+"...";
    }
    holder.nomePraia.setText(Html.fromHtml("<b><i>"+categoria.nome+"<i/><b/>"));
    holder.descricao.setText(Html.fromHtml(desc));
   /* if(categoria.nome.length()>20){
        holder.nomePraia.setHeight(200);
        Log.e("entrou",categoria.nome);
    }*/
    if (categoria.favorito) {
        holder.btFavorito.setImageResource(R.drawable.ic_action_favorite_v);
    } else {
        holder.btFavorito.setImageResource(R.drawable.ic_action_favorite);
    }

The class that loads the image on that line bitmapTask.execute(drawableId);

Loadbitmapimage.java

public class LoadBitmapImage extends AsyncTask<Integer, Void, Bitmap> {
private final WeakReference<ImageView> imageViewReference;
private View view;
private final int width;
private Context context;

public LoadBitmapImage(View view,ImageView imageView,int width,Context context) {
    this.view = view;
    this.width = width;
    this.context = context;
    // Use a WeakReference para ter certeza que sua ImageView sera reciclada
    imageViewReference = new WeakReference<ImageView>(imageView);
}

// Carregando a imagem em background
@Override
protected Bitmap doInBackground(Integer... drawableId) {
    // Aqui voce utiliza seus proprios metodos para carregar e configurar sua Bitmap
    Bitmap bm = BitmapFactory.decodeResource(context.getResources(), drawableId[0]);
    bm = bm.createScaledBitmap(bm, width, width, true);
    return bm;
}

// Quando completo, coloque o Bitmap em sua ImageView
@Override
protected void onPostExecute(Bitmap bitmap) {
    if (imageViewReference != null && bitmap != null) {
        final ImageView imageView = imageViewReference.get();
        AdapterListView.ViewHolder holder;
        holder = (AdapterListView.ViewHolder)view.getTag();
        if (imageView != null) {
            holder.foto.setImageBitmap(bitmap);
        }
        holder.pb.setVisibility(View.INVISIBLE);
    }
}

}

This way is loading the normal image , but has a problem . When you pass the listview very fast and stop in some image you load the wrong image and show the images before showing the right image, I wanted to know if there is a way when I call the class to load the image I link the image to an item so that it even happens fast and have more than one image clicking know which item to put in order not to put the image in the wrong item

1 answer

-1


Browser other questions tagged

You are not signed in. Login or sign up in order to post.