当前位置: 动力学知识库 > 问答 > 编程问答 >

Android gridview : Trying to reproduce interface + lags

问题描述:

I have 2 problems :

  • I am trying to reproduce an interface (see below)
  • My gridview lag so much

I would like to reproduce this interface : Each item has a random height of ImageView.

Today here is my result :

What is best way to create gridview like that ?

Here is the code of my adapter :

import java.util.ArrayList;

import java.util.List;

import java.util.Random;

import android.app.Activity;

import android.graphics.Typeface;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;

import com.nostra13.universalimageloader.core.ImageLoader;

import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class AllAnnonces_custom_adapter extends ArrayAdapter<Annonce> {

Typeface robotolight = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");

Typeface robotoBold = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf");

Typeface robotoRegular = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf");

ArrayList<Integer> lista = new ArrayList<Integer>();

public AllAnnonces_custom_adapter(Activity activity, List<Annonce> annonces) {

super(activity, 0, annonces);

lista.add(120);

lista.add(170);

lista.add(220);

}

@Override

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

Activity activity = (Activity) getContext();

LayoutInflater inflater = activity.getLayoutInflater();

View rowView;

Annonce annonce = getItem(position);

//rowView = inflater.inflate(R.layout.all_annonces_item, null);

rowView = inflater.inflate(R.layout.grid_single, null);

////////////////////////////////////////////////////////

///// IMAGE

///////////////////////////////////////////////////////

//ImageView imageAnnonce = (ImageView) rowView.findViewById(R.id.allannonces_image) ;

ImageView imageAnnonce = (ImageView) rowView.findViewById(R.id.grid_image) ;

Random r = new Random();

imageAnnonce.getLayoutParams().height = lista.get(r.nextInt(lista.size()));

DisplayImageOptions options = new DisplayImageOptions.Builder()

.cacheInMemory(true)

.cacheOnDisc(true)

.build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getContext())

.defaultDisplayImageOptions(options)

.threadPriority(Thread.MAX_PRIORITY)

.denyCacheImageMultipleSizesInMemory()

.memoryCacheSize(2 * 1024 * 1024)

.build();

ImageLoader imageLoader = ImageLoader.getInstance();

imageLoader.init(config);

imageLoader.displayImage(annonce.getImage(), imageAnnonce);

////////////////////////////////////////////////////////

///// TITLE

///////////////////////////////////////////////////////

//TextView titre = (TextView) rowView.findViewById(R.id.allannonces_titre);

TextView titre = (TextView) rowView.findViewById(R.id.grid_text);

titre.setText(annonce.getTitle());

titre.setTypeface(robotoRegular);

////////////////////////////////////////////////////////

///// VILLE OBJET

///////////////////////////////////////////////////////

//TextView villeObj = (TextView) rowView.findViewById(R.id.allannonces_ville);

TextView villeObj = (TextView) rowView.findViewById(R.id.grid_ville);

villeObj.setText(annonce.getVilleObjet());

villeObj.setTypeface(robotoRegular);

////////////////////////////////////////////////////////

///// PRIX

///////////////////////////////////////////////////////

//TextView prix = (TextView) rowView.findViewById(R.id.allannonces_prix);

TextView prix = (TextView) rowView.findViewById(R.id.grid_prix);

prix.setText("€" + annonce.getPrix() + ",00 EUR");

prix.setTypeface(robotoRegular);

return rowView;

}

}

[UPDATE]

New code of adapter after remarks :

import android.app.Activity;

import android.graphics.Typeface;

import android.view.LayoutInflater;

import android.view.View;

import android.view.ViewGroup;

import android.widget.ArrayAdapter;

import android.widget.ImageView;

import android.widget.TextView;

import com.nostra13.universalimageloader.core.DisplayImageOptions;

import com.nostra13.universalimageloader.core.ImageLoader;

import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

public class AllAnnonces_custom_adapter extends ArrayAdapter<Annonce> {

Typeface robotolight = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Light.ttf");

Typeface robotoBold = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Bold.ttf");

Typeface robotoRegular = Typeface.createFromAsset(getContext().getAssets(), "fonts/Roboto-Regular.ttf");

public AllAnnonces_custom_adapter(Activity activity, List<Annonce> annonces) {

super(activity, 0, annonces);

}

@Override

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

ViewHolderItem viewHolder;

if (convertView==null) {

Activity activity = (Activity) getContext();

// inflate the layout

LayoutInflater inflater = activity.getLayoutInflater();

convertView = inflater.inflate(R.layout.grid_single, parent, false);

// well set up the ViewHolder

viewHolder = new ViewHolderItem();

viewHolder.imageAnnonce = (ImageView) convertView.findViewById(R.id.grid_image) ;

viewHolder.titre = (TextView) convertView.findViewById(R.id.grid_text);

viewHolder.villeObj = (TextView) convertView.findViewById(R.id.grid_ville);

viewHolder.prix = (TextView) convertView.findViewById(R.id.grid_prix);

// store the holder with the view.

convertView.setTag(viewHolder);

} else {

viewHolder = (ViewHolderItem) convertView.getTag();

}

Annonce annonce = getItem(position);

if (annonce != null) {

DisplayImageOptions options = new DisplayImageOptions.Builder()

.cacheInMemory(true)

.cacheOnDisc(true)

.build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(getContext())

.defaultDisplayImageOptions(options)

.threadPriority(Thread.MAX_PRIORITY)

.denyCacheImageMultipleSizesInMemory()

.memoryCacheSize(2 * 1024 * 1024)

.build();

ImageLoader imageLoader = ImageLoader.getInstance();

imageLoader.init(config);

imageLoader.displayImage(annonce.getImage(), viewHolder.imageAnnonce);

viewHolder.titre.setText(annonce.getTitle());

viewHolder.titre.setText(annonce.getTitle());

viewHolder.titre.setTypeface(robotoRegular);

viewHolder.villeObj.setText(annonce.getVilleObjet());

viewHolder.villeObj.setText(annonce.getVilleObjet());

viewHolder.villeObj.setTypeface(robotoRegular);

viewHolder.prix.setText(annonce.getPrix());

viewHolder.prix.setText("€" + annonce.getPrix() + ",00 EUR");

viewHolder.prix.setTypeface(robotoRegular);

}

return convertView;

}

static class ViewHolderItem {

ImageView imageAnnonce;

TextView titre;

TextView villeObj;

TextView prix;

}

}

分享给朋友:
您可能感兴趣的文章:
随机阅读: