Android gridview:尝试重现界面+滞后

时间:2014-06-02 11:49:14

标签: android android-layout gridview android-gridview

我有两个问题:

  • 我正在尝试重现界面(见下文)
  • 我的网格视图滞后太多

我想重现这个界面:每个项目都有一个随机高度的ImageView。

enter image description here

今天是我的结果:

enter image description here

创建gridview的最佳方法是什么?

以下是我的适配器的代码:

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]

备注后的新适配器代码:

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;
    }


}

0 个答案:

没有答案