我是新来的,从事 Android 开发。
我正在尝试使用ViewPager
.
这是我的主要活动:
public class HomeActivity extends AbstractActivity {
ArrayList<ImageView> imagens = new ArrayList<ImageView>();
ViewPager slideshow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
slideshow = (ViewPager) findViewById(R.id.slideshow);
// inicia a task json
new aTask(this).execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_home, menu);
return true;
}
public void getResult(String result) {
Gson gson = new Gson();
Home home = gson.fromJson(result, Home.class);
imagens = new ArrayList<ImageView>();
for (Webdoor webdoor : home.webdoors) {
try {
ImageView img = new ImageView(getApplicationContext());
Bitmap bitmap = BitmapFactory
.decodeStream((InputStream) new URL(webdoor.imagem)
.getContent());
img.setImageBitmap(bitmap);
imagens.add(img);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private class aTask extends AsyncTask<Void, Void, ArrayList<ImageView>> {
private HomeActivity homeActivityRef;
public aTask(HomeActivity homeActivity) {
this.homeActivityRef = homeActivity;
}
// inicio da task
@Override
protected void onPreExecute() {
}
// execução da tarefa em background
@Override
protected ArrayList<ImageView> doInBackground(Void... params) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.getMessageConverters().add(
new StringHttpMessageConverter());
String result = restTemplate.getForObject(
"http://api.modahub.com.br/home", String.class,
"SpringSource");
Gson gson = new Gson();
Home home = gson.fromJson(result, Home.class);
ArrayList<ImageView> imagens = new ArrayList<ImageView>();
for (Webdoor webdoor : home.webdoors) {
try {
ImageView img = new ImageView(getApplicationContext());
Bitmap bitmap = BitmapFactory
.decodeStream((InputStream) new URL(webdoor.imagem)
.getContent());
img.setImageBitmap(bitmap);
imagens.add(img);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
publishProgress(); // invoca o onProgressUpdate()
return imagens;
}
// callback invocado pelo publishProgress()
@Override
protected void onProgressUpdate(Void... values) {
}
// callback executado no final da task
@Override
protected void onPostExecute(ArrayList<ImageView> aImagens) {
// This variable aImagens is full of images!
// How I put this on the screen =(
}
}
}
如何使ImageView
s in列表onPostExecute()
在屏幕上呈现?我需要 aFragment
还是 a Loader
?