4
I have a problem I don’t know how to solve, I have a ListView
customized with images and the scroll bar keeps crashing, even with the list loaded. It slows down the list to scroll.
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 filling the 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?
What’s in the classes
Segmento
andCategoria
?– Victor Stafusa
hello, I changed the post because Adapter was wrong, there is no Segment class, only category.Well, Category is just a class where it contains Title, and Image, and the get and set, is just a model form.
– Clara Campos
After scrolling the list all images remain loaded and the scrolling gets better?
– Jorge B.
No, it’s always slow even when the images are loaded.
– Clara Campos
It’s probably the image’s code. Already tried to put the line
((ImageView) view.findViewById(R.id.imagemview)).setImageResource(item.getIconeRid());
within aAsyncTask
?– rsicarelli