简体   繁体   中英

Android - How to know when getView is called to inflate list

I have a List Adapter which inflates a list. On the method getView() i make an AscyncTask to get an image from internet. However, i figured out that the method getView() is called for numerous reasons, not only to inflate the list.

My question is: how to know when the method is called to inflate the list? I don't want to make a AsyncTask every time the method is called.

Here is my code:

public class ListItem extends ArrayAdapter<Carro> {

    private final Activity context;
    private final List<Carro> carros;

    public ListItem(Activity context, List<Carro> carros) {
        super(context, R.layout.list_item, carros);
        this.context = context;
        this.carros = carros;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = context.getLayoutInflater();
        View rowView = inflater.inflate(R.layout.list_item, null, true);

        TextView modelo = (TextView) rowView.findViewById(R.id.modelo);
        modelo.setText(carros.get(position).getModelo());

        GetFoto getFoto = new GetFoto(rowView);
        getFoto.execute(position);
        return rowView;
    }

    private class GetFoto extends AsyncTask<Integer, Void, Void> {

        Drawable fotoDraw;
        View rowView;
        int position;

        public GetFoto(View view) {
            this.rowView = view;
        }

        @Override
        protected Void doInBackground(Integer... params) {
            position = params[0];
            HTTPHandler handler = new HTTPHandler();

            fotoDraw = handler.makeServiceCallImage(carros.get(position).getFoto());
            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            ImageView imagem = (ImageView) rowView.findViewById(R.id.imagem);
            imagem.setImageDrawable(fotoDraw);
        }
    }
}

The method getView() will be called every time the view appears on the screen during the scrolling process. The number of getView() method calls can be greater than the number of elements if the ListView height is set to wrap_content .

Use some libraries like Picasso or Glide for downloading images. These libraries will cache the downloaded images that prevents unnecessary requests.

It will look like this:

Glide.with(context)
     .load(imageUrl)
     .into(imageView);

Or you can read this article https://developer.android.com/topic/performance/graphics/cache-bitmap.html

I recommend using RecyclerView instead of ListView . It recycling the views which prevents multiple inflation

Like Alex Nik said: use picasso or glide for picture loads. And stop using ListViews, they are kind of deprecated, yet still supported. Use RecyclerView, it is better and easier to use.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM