Android how to stop reading getView () function again if content is already loaded in ListView

I am a guy for Android newbies.

I have a ListView displaying images for each ListView item, it works fine ... but when I start scrolling, I realized that my image is loading again if it is displayed on the screen of my telephone device!

How can I reload images or all contents in ListView again? Or how could I not read the getView () function again. If I have already downloaded all its contents?

+3
source share
3 answers

To expand CaseyB's offer, I would recommend that you look at Turbo-charge your user interface.

, , .

50:00 -. .

+3

! , ...

  static class ViewHolder {
    TextView text;
    ImageView icon;
    }
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_icon_text, null);
holder = new ViewHolder();
holder.text = (TextView) convertView.findViewById(R.id.text);
holder.icon = (ImageView) convertView.findViewById(R.id.icon);

 convertView.setTag(holder);
 } else {
 holder = (ViewHolder) convertView.getTag();
 }

 holder.text.setText(DATA[position]);
 holder.icon.setImageBitmap((position & 1) == 1 ? mIcon1 : mIcon2);

 return convertView;
 }

Turbo-Chargeyour

3 ListView! 0, 5 ...

public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;

convertView = null; 
//****FORCING TO CREATE EVERYTIME A DIFFERENT VIEW! ( I USE 3 DIFERENT TYPES OF VIEWS)
if (convertView == null) {

if (position == 0) {
        LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = vi.inflate(R.layout.iconrowmain, null);
} else if (position == 5) {
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = vi.inflate(R.layout.iconrowwebadvice, null);          
} else {
    LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    convertView = vi.inflate(R.layout.iconrowa, null);
}
}

!:( : (

, getView(), !????

! Alexi

P.S. ! !!!: D

+1

You can create an effective adapter with different types of views. Take a look at this question .

It is pretty simple. Just need to override getItemViewType and getViewTypeCount. Then you can count on convertView to be the right kind.

0
source

Source: https://habr.com/ru/post/1727416/


All Articles