ListView too slow on scrollbar

4

I have a problem that I do not know how to solve, I have a custom% with_configure with images and the scroll bar keeps crashing even with the loaded list. The list is slow to roll.

Adapter:

public class AdapterSegmento extends BaseAdapter {

private LayoutInflater mInflater;
private ArrayList<Categoria> itens;

public AdapterListView(Context context, ArrayList<Categoria> itens) {
    //Itens que preencheram o listview
    this.itens = itens;
    //responsavel por pegar o Layout do item.
    mInflater = LayoutInflater.from(context);
}

/**
 * Retorna a quantidade de itens
 *
 * @return
 */
public int getCount() {
    return itens.size();
}

/**
 * Retorna o item de acordo com a posicao dele na tela.
 *
 * @param position
 * @return
 */
public Categoria getItem(int position) {
    return itens.get(position);
}

/**
 * Sem implementação
 *
 * @param position
 * @return
 */
public long getItemId(int position) {
    return position;
}

public View getView(int position, View view, ViewGroup parent) {
    //Pega o item de acordo com a posção.
    Categoria item = itens.get(position);
    //infla o layout para podermos preencher os dados
    view = mInflater.inflate(R.layout.item_list, null);

    //atravez do layout pego pelo LayoutInflater, pegamos cada id relacionado
    //ao item e definimos as informações.
    ((TextView) view.findViewById(R.id.text)).setText(item.getTexto());
    ((ImageView) view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());
  //  ((TextView) view.findViewById(R.id.subtitulo)).setText(item.getSubtitulo());

    return view;
}

Class that populates list:

private void gerarLista() {

    itens = new ArrayList<Categoria>();

    String[] categorias = getResources().getStringArray(R.array.categorias);
    String[] drawableCategorias = getResources().getStringArray(
            R.array.categorias_drawable);

    listView.setScrollingCacheEnabled(false);

    // Vai recuperar os dois array's la do strings.xml e iterar sobre eles e criar os itens
    for (int i = 0; i < categorias.length; ++i) {
        itens.add(new Categoria(categorias[i], getResources()
                .getIdentifier(drawableCategorias[i], "drawable",
                        this.getPackageName())));
    }

    // Criamos uma lista que preenchera o ListView
    /*
     * itens = new ArrayList<Categoria>(); Categoria item1 = new
     * Categoria("Alimentação", R.drawable.alimentacao); Categoria item2 =
     * new Categoria("Esporte", R.drawable.esporte);
     * 
     * itens.add(item1); itens.add(item2);
     */
    // Cria o adapter
    adapterListView = new AdapterListView(this, itens);

    // Define o Adapter
    listView.setAdapter(adapterListView);
    // Cor quando a lista é selecionada para ralagem.
    listView.setCacheColorHint(Color.TRANSPARENT);
}

How to solve this?

    
asked by anonymous 02.12.2014 / 05:06

1 answer

4

The problem lies in the architecture of your algorithm. At each iteration in the% method of the Adapter you are inflating the view you want. This is bad practice!

You need to use the Holder project pattern, which will statically save your Views and will improve performance. Also, if you realize, the getView() method is parameterized by View view that is nothing more than View that you return in this very method, that is, they use the recursion methodology.

A very basic example of the Holder pattern would be:

public static class ViewHolder {
  TextView hTextViewName;
}

public View getView(int position, View view, ViewGroup parent) {
  ViewHolder holder;
  if (view == null) {
    holder = new ViewHolder();
    view = LayoutInflater.from(context).inflate(R.layout.view_to_inflate, parent, false);
    holder.hTextViewName = (TextView) view.findViewById(R.id.textView_name);
    view.setTag(holder);
  } else {
    holder = (ViewHolder) view.getTag();
  }

  String name = getItem(position);
  holder.hTextViewName.setText(name);

  return view;
}

Notice the difference! This way you will have much more performance.

In short, from the case above you just need to adapt it to your context! : -)

    
02.12.2014 / 14:47