This could be done in the following steps:
- Create AsyncTaskLoader (here is a good tutorial http://www.androiddesignpatterns.com/2012/08/implementing-loaders.html). There you can load your images from the server.
- Create custom adapter for storing your images and creating views by extending
BaseAdapter
, for instance. Make your own implementation of needed functions. - Create a function inside this adapter responsible for adding new data. Don't forget to call
notifyDataSetChanged()
once you are done - Attach this adapter to your
GridView
- Push loaded images into you adapter.
EDIT Ok, here is a basic Proof of concept:
Activity (loader can be started at the onCreate
method):
public class MyActivity extends Activity implements LoaderManager.LoaderCallbacks<ArrayList<Drawable>> {
//other stuff
@Override
public Loader<ArrayList<Drawable>> onCreateLoader(int id, Bundle args) {
return new ImageLoader(this);
}
@Override
public void onLoadFinished(Loader<ArrayList<Drawable>> loader, ArrayList<Drawable> data) {
myAdapter.pushData(data);
}
@Override
public void onLoaderReset(Loader<ArrayList<Drawable>> loader) {
}
}
Loader:
public class ImageLoader extends AsyncTaskLoader<ArrayList<Drawable>> {
public ImageLoader(Context context) {
super(context);
}
@Override
public ArrayList<Drawable> loadInBackground() {
//load the stuff
return data;
}
@Override
protected void onStartLoading() {
if (data != null) {
deliverResult(data);
}
if (takeContentChanged() || data == null) {
forceLoad();
}
}
}
Adapter:
public class MyAdapter extends BaseAdapter {
ArrayList<Drawable> data = new ArrayList<>();
//other functions
public void pushData(ArrayList<Drawable> data){
this.data = data;
notifyDataSetChanged();
}
}