GridView第一次不显示图像

时间:2014-07-22 13:34:42

标签: android gridview gallery aquery

我有一个GridView,我用它来构建一个库,但我有这个问题,我第一次调用图库活动时图像没有显示,但如果我按BackButton然后再次打开活动, gridview显示图像(如果旋转屏幕,图像也显示)。 我正在从网址下载图片。

以下是图库的代码:

public class Galeria extends ActionBarActivity {

    private AdapterGaleria adapter;
    private ArrayList<Foto> fotos;
    private GridView grid;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_galeria);

        getSupportActionBar().setHomeButtonEnabled(true);
        getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar_background));
        getSupportActionBar().setIcon(getResources().getDrawable(R.drawable.icon));

        Bundle b = getIntent().getExtras();

        final Fotos fts = (Fotos)b.getSerializable("photos");

        fotos = ((Fotos)b.getSerializable("photos")).getFotos();
        grid = (GridView)findViewById(R.id.grid_galeria);
        adapter = new AdapterGaleria(getApplicationContext(), R.layout.item_galeria, fotos);
        grid.setAdapter(adapter);
        grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub
                Intent it = new Intent(Galeria.this, GaleriaAberta.class);
                it.putExtra("position", position);
                it.putExtra("photos", fts);
                startActivity(it);

            }
        });
    }
}

这是适配器的代码:

public class AdapterGaleria extends ArrayAdapter<Foto>{

    private LayoutInflater inflater;
    private Context context;
    private ArrayList<Foto> imagesToLoad;

    public AdapterGaleria(Context context, int resource, List<Foto> objects) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
        inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        this.imagesToLoad = (ArrayList<Foto>)objects;
        this.context = context;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imagesToLoad.size();
    }

    @Override
    public Foto getItem(int position) {
        // TODO Auto-generated method stub
        return imagesToLoad.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class ViewHolder{

        ImageView image;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.d("GET_VIEW", "get_view");
        Foto f = new Foto();
        f = getItem(position);
        ViewHolder holder;
        View v;
        v = convertView;
        if (v == null){
            holder = new ViewHolder();
            v = inflater.inflate(R.layout.item_galeria, parent, false);
            holder.image = (ImageView)v.findViewById(R.id.image_galeria);
            v.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        AQuery aq = new AQuery(context);
        aq.id(holder.image).image(imagesToLoad.get(position).getNormal(), true, false, 300, 0, null,0,1.0f / 1.0f);

        return v;
    }

}

2 个答案:

答案 0 :(得分:0)

在您的适配器中使用资源ID,您从构造函数获取并检查它是否有效

public class AdapterGaleria extends ArrayAdapter<Foto>{

    private LayoutInflater inflater;
    private Context context;
    private ArrayList<Foto> imagesToLoad;
    private int resource;

    public AdapterGaleria(Context context, int resource, List<Foto> objects) {
        super(context, resource, objects);
        // TODO Auto-generated constructor stub
        inflater = (LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        this.imagesToLoad = (ArrayList<Foto>)objects;
        this.context = context;
        this.resource = resource;
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return imagesToLoad.size();
    }

    @Override
    public Foto getItem(int position) {
        // TODO Auto-generated method stub
        return imagesToLoad.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    public class ViewHolder{

        ImageView image;

    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        Log.d("GET_VIEW", "get_view");
        Foto f = new Foto();
        f = getItem(position);
        ViewHolder holder;
        View v;
        v = convertView;
        if (v == null){
            holder = new ViewHolder();
            v = inflater.inflate(resource, parent, false);
            holder.image = (ImageView)v.findViewById(R.id.image_galeria);
            v.setTag(holder);
        }else{
            holder = (ViewHolder)convertView.getTag();
        }

        AQuery aq = new AQuery(context);
        aq.id(holder.image).image(imagesToLoad.get(position).getNormal(), true, false, 300, 0, null,0,1.0f / 1.0f);

        return v;
    }

}

答案 1 :(得分:0)

我只需要在AQuery完成下载之前将预加载图像传递给视图,然后在下载完成后,图像将显示在gridview中。 :)