我想以一种方式在画廊视图中加载图像,即一旦加载图像而不是在用户滚动之后,想要显示加载指示器,而不是在显示新图像之后。在我下面的代码中,它需要所有数据(包括图像)并显示。
这是我的代码:
ProductDetailsActivity.java
public class ProductsDetailActivity extends BaseActivity implements
OnClickListener, OnItemSelectedListener {
Gallery gallery;
TextView prod_descri, productTitle1, productTitle2;
ImageAdapter imageAdapter;
ImageView leftArrow, rightArrow;
private int selectedImagePosition = 0;
private List<Drawable> drawables;
String title, url, title1;
Bundle b;
Products product;
ProductDetails pDetails;
ArrayList<ProductDetails> productList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.products_details);
init();
setTitle(getResources().getString(R.string.title_product));
b = getIntent().getExtras();
product = (Products) b.getSerializable("product");
if (b != null) {
if (Global.isNetworkAvailable(ProductsDetailActivity.this)) {
new ProductAsyncTask().execute(Constants.url,
product.product_id);
} else {
Intent intent = new Intent(ProductsDetailActivity.this,
OfflineActivity.class);
startActivity(intent);
}
} else {
Log.v("Bundle", "Bundle null");
}
}
private void init() {
productList = new ArrayList<ProductDetails>();
gallery = (Gallery) findViewById(R.id.product_gallery);
prod_descri = (TextView) findViewById(R.id.product_desc);
productTitle1 = (TextView) findViewById(R.id.productTitle1);
productTitle2 = (TextView) findViewById(R.id.productTitle2);
leftArrow = (ImageView) findViewById(R.id.left_arrow_imageview);
rightArrow = (ImageView) findViewById(R.id.right_arrow_imageview);
leftArrow.setOnClickListener(this);
rightArrow.setOnClickListener(this);
gallery.setOnItemSelectedListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.left_arrow_imageview:
if (selectedImagePosition > 0) {
// leftArrow.setVisibility(View.VISIBLE);
--selectedImagePosition;
} else if (selectedImagePosition == 0) {
leftArrow.setVisibility(View.INVISIBLE);
rightArrow.setVisibility(View.VISIBLE);
}
gallery.setSelection(selectedImagePosition, true);
break;
case R.id.right_arrow_imageview:
if (selectedImagePosition < drawables.size() - 1) {
// rightArrow.setVisibility(View.VISIBLE);
++selectedImagePosition;
} else if (selectedImagePosition == drawables.size() - 1) {
rightArrow.setVisibility(View.INVISIBLE);
leftArrow.setVisibility(View.VISIBLE);
}
gallery.setSelection(selectedImagePosition, true);
break;
default:
break;
}
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
selectedImagePosition = position;
productTitle1.setText(product.title);
productTitle2.setText(product.title);
prod_descri.setText(product.long_description);
if (selectedImagePosition > 0
&& selectedImagePosition < drawables.size() - 1) {
leftArrow.setVisibility(View.VISIBLE);
rightArrow.setVisibility(View.VISIBLE);
} else if (selectedImagePosition == 0 && drawables.size() > 1) {
leftArrow.setVisibility(View.INVISIBLE);
rightArrow.setVisibility(View.VISIBLE);
} else if (selectedImagePosition == drawables.size() - 1
&& drawables.size() > 1) {
rightArrow.setVisibility(View.INVISIBLE);
leftArrow.setVisibility(View.VISIBLE);
} else if (drawables.size() == 1) {
rightArrow.setVisibility(View.INVISIBLE);
leftArrow.setVisibility(View.INVISIBLE);
}
// setSelectedImage(selectedImagePosition);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
// AsyncTask
public class ProductAsyncTask extends
AsyncTask<String, String, ArrayList<ProductDetails>> {
private Context getDialogContext() {
Context context;
if (getParent() != null)
context = getParent();
else
context = ProductsDetailActivity.this;
return context;
}
String responseString = null;
ProgressDialog progressDialog;
public ProductAsyncTask() {
super();
// progressDialog = new ProgressDialog(ProductsDetailActivity.this);
progressDialog = new ProgressDialog(getDialogContext());
progressDialog.setCancelable(false);
}
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog.setMessage("Loading...");
progressDialog.show();
}
@Override
protected ArrayList<ProductDetails> doInBackground(String... params) {
responseString = readJSONSFeed(params[0], params[1]);
if (responseString != null) {
return processProductDetailsJSON(responseString);
} else {
return null;
}
}
@Override
protected void onPostExecute(ArrayList<ProductDetails> result) {
super.onPostExecute(result);
try {
if (result.size() == 0 || result == null) {
}
progressDialog.dismiss();
drawables = createDrawables(result);
imageAdapter = new ImageAdapter(ProductsDetailActivity.this,
drawables);
if (drawables.size() > 0) {
gallery.setSelection(selectedImagePosition, true);
}
if (drawables.size() == 1) {
leftArrow.setVisibility(View.INVISIBLE);
rightArrow.setVisibility(View.INVISIBLE);
}
gallery.setAdapter(imageAdapter);
} catch (Exception e) {
e.printStackTrace();
}
}
private List<Drawable> createDrawables(ArrayList<ProductDetails> result) {
List<Drawable> drawablesUrl = new ArrayList<Drawable>();
try {
for (ProductDetails objProDetails : result) {
drawablesUrl.add(Global.getBitmapFromURL(
ProductsDetailActivity.this,
objProDetails.image_path));
// drawablesUrl.add(loadImageFromURL(objProDetails.image_path));
}
return drawablesUrl;
} catch (Exception e) {
return null;
}
}
@SuppressWarnings("unused")
private Drawable loadImageFromURL(String image_path) {
try {
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
} catch (Exception e) {
return getResources().getDrawable(R.drawable.noimage);
}
}
private String readJSONSFeed(String url, String productID) {
StringBuilder stringBuilder = new StringBuilder();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url + Constants.REQ_FOR_PRODUCT_IMAGE
+ productID + Constants.REQ_FOR_OFFER);
httpGet.setHeader("Content-Type", "application/json");
httpGet.setHeader("password", "password");
httpGet.setHeader("username", "user");
try {
HttpResponse response = httpClient.execute(httpGet);
StatusLine statusLine = response.getStatusLine();
int statuscode = statusLine.getStatusCode();
if (statuscode == 200) {
HttpEntity httpEntity = response.getEntity();
InputStream inputStream = httpEntity.getContent();
BufferedReader buffer = new BufferedReader(
new InputStreamReader(inputStream));
String line = null;
if ((line = buffer.readLine()) != null) {
stringBuilder.append(line);
}
// buffer.close();
inputStream.close();
} else {
}
} catch (Exception e) {
}
return stringBuilder.toString();
}
private synchronized ArrayList<ProductDetails> processProductDetailsJSON(
String response) {
try {
JSONObject json = new JSONObject(response);
JSONArray Records = json
.getJSONArray(Constants.JSON_OBJECT_DATA);
for (int i = 0; i < Records.length(); i++) {
JSONObject Record = Records.getJSONObject(i);
String tTitle = Record.getString("title");
String tImagePath = Record.getString("image_path");
String tDescription = Record.getString("description");
tImagePath = Constants.REQ_IMAGE_PRODUCT + tImagePath;
pDetails = new ProductDetails(tDescription, tTitle,
tImagePath);
if (pDetails == null) {
} else {
productList.add(pDetails);
}
}
} catch (JSONException ex) {
System.out.println(ex);
return null;
}
return productList;
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Activity context;
ImageView imageView;
private List<Drawable> totalImages;
private static ViewHolder holder;
ArrayList<Object> imageGallery;
int width;
int height;
Display display;
int[] images = { R.drawable.ic_launcher, R.drawable.ic_launcher };
public ImageAdapter(Context c, List<Drawable> drawables) {
this.context = (Activity) c;
imageGallery = new ArrayList<Object>();
this.totalImages = drawables;
display = context.getWindowManager().getDefaultDisplay();
// this.imageGallery = objects;
// width = display.getWidth();
// height = display.getHeight();
}
@Override
public int getCount() {
return totalImages.size();
// return 1;
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
try {
if (convertView == null) {
holder = new ViewHolder();
imageView = new ImageView(this.context);
convertView = imageView;
holder.imageView = imageView;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageView.setImageDrawable(totalImages.get(position));
holder.imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT));
holder.imageView.setScaleType(ImageView.ScaleType.FIT_XY);
return imageView;
} catch (Exception e) {
e.printStackTrace();
return imageView;
}
}
static class ViewHolder {
public ViewHolder() {
}
ImageView imageView;
}
}
Here is my CustomArrayAdapter
CustomArrayAdapter.java
public class CustomArrayAdapter extends ArrayAdapter<Object> {
Context mContext;
ArrayList<Products> product;
int layoutId;
ImageLoader imageLoader;
public CustomArrayAdapter(Context context, int textViewResourceId,
ArrayList<Products> objects) {
super(context, textViewResourceId);
this.mContext = context;
this.layoutId = textViewResourceId;
this.product = objects;
imageLoader = new ImageLoader(context, R.drawable.noimage);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
LayoutInflater layoutInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(layoutId, parent, false);
holder = new ViewHolder(convertView);
holder.setValues(product.get(position));
return convertView;
}
public int getCount() {
return product.size();
}
public class ViewHolder {
TextView tvProductTitle;
TextView tvProductDescription;
ImageView ivProductLogo, imgArrow;
public ProgressBar progress;
public ViewHolder(View v) {
tvProductTitle = (TextView) v.findViewById(R.id.tvTitle);
tvProductDescription = (TextView) v
.findViewById(R.id.tvDescriptions);
ivProductLogo = (ImageView) v.findViewById(R.id.ivLogoProduct);
imgArrow = (ImageView) v.findViewById(R.id.imgArrow);
progress = (ProgressBar) v.findViewById(R.id.progress_bar);
}
public void setValues(Products products) {
tvProductTitle.setText(products.title);
tvProductDescription.setText(products.short_description);
// imageLoader.displayImage(products.image, imageView, options);
try {
ImageLoader imageLoader = new ImageLoader(mContext);
DisplayImageOptions options = new DisplayImageOptions.Builder()
.cacheOnDisc().build();
imageLoader.DisplayImage(products.image, ivProductLogo);
if (Global.fromProduct) {
imgArrow.setImageDrawable(mContext.getResources().getDrawable(R.drawable.cell_arrow_right));
} else {
imgArrow.setImageDrawable(mContext.getResources().getDrawable(R.drawable.info_btn));
}
} catch (Exception e) {
e.printStackTrace();
}
// try {
}
@SuppressWarnings("unused")
private boolean cancelPotentialDownload(String url, ImageView imageView) {
BitmapDownloaderTask bitmapDownloaderTask = getBitmapDownloaderTask(imageView);
if (bitmapDownloaderTask != null) {
String bitmapUrl = bitmapDownloaderTask.url;
if ((bitmapUrl == null) || (!bitmapUrl.equals(url))) {
bitmapDownloaderTask.cancel(true);
} else {
// The same URL is already being downloaded.
return false;
}
}
return true;
}
private BitmapDownloaderTask getBitmapDownloaderTask(ImageView imageView) {
if (imageView != null) {
Drawable drawable = imageView.getDrawable();
if (drawable instanceof DownloadedDrawable) {
DownloadedDrawable downloadedDrawable = (DownloadedDrawable) drawable;
return downloadedDrawable.getBitmapDownloaderTask();
}
}
return null;
}
}
}'