My questions are about delay image loading using AQuery
所以我的应用程序是这样的:我读了一个json,它包含各种信息和图像链接(这是在AsyncTask的doInBackground程序中完成的)。此外,在阅读此过程中的链接后,我还阅读了图片(请参阅下面的代码了解更多信息)
class GetMessages extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... params) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(uri);
// code that reads the json .......
Bitmap picture = null;
try {
URL newurl = new URL(json.getString("pic_url"));
picture = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// after this, i put all the read info, and the pics in a list of
// objects, that is then usen in the method onPostExecute to populate
// my custom adapter ...see code below
static class ViewHolder {
ImageView picView;
}
public class MyCustomBaseAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<Product> products;
public ArrayList<Product> getProducts() {
return products;
}
public void setProducts(ArrayList<Product> products) {
this.products = products;
}
public MyCustomBaseAdapter(Context context, ArrayList<Product> products) {
this.products = products;
this.mInflater = LayoutInflater.from(context);
}
public int getCount() {
return products.size();
}
public Object getItem(int position) {
return products.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.product_item, null);
holder = new ViewHolder();
holder.picView = (ImageView) convertView.findViewById(R.id.pic_view);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.picView.setImageBitmap(products.get(position).getPicture());
return convertView;
}
正如你已经可以弄明白的......如果我有15张照片......用户在加载所有信息之前就会感到困倦......
谢谢:)
答案 0 :(得分:0)
以下是使用shouldDelay的演示源:
https://github.com/androidquery/androidquery/blob/master/demo/src/com/androidquery/test/image/ImageLoadingListActivity.java https://github.com/androidquery/androidquery/blob/master/demo/src/com/androidquery/test/image/ImageLoadingList4Activity.java
尝试一步一步。
我建议您在getView方法中替换几行,如示例所示,并使用aq.image将图片加载到您的网址。
替换:
holder.picView.setImageBitmap(products.get(position).getPicture());
with:
AQuery aq = new AQuery(convertView);
aq.id(holder.picView).image(products.get(position).getPicture());
之后添加shouldDelay以提高效率。
答案属于Peter Liu,他是关于这个框架的大师之一:)