1
我試圖在列表視圖上顯示圖像。在一個單元中,我一次顯示4個圖像。當我不使用AsynkTask時,它工作,但不是平滑滾動。所以我試圖使用Asynktasks。但在我的實施中,它什麼也沒有顯示。ListView與AsyncTask不起作用
沒有的AsyncTask
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private File[] data;
private LayoutInflater inflater = null;
public LazyAdapter(Activity a, File[] files) {
activity = a;
data=files;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.length/4 + 1;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.gallery_item, null);
holder = new ViewHolder();
holder.icon1 = (ImageView) convertView.findViewById(R.id.imageView1);
holder.icon2 = (ImageView) convertView.findViewById(R.id.imageView2);
holder.icon3 = (ImageView) convertView.findViewById(R.id.imageView3);
holder.icon4 = (ImageView) convertView.findViewById(R.id.imageView4);
holder.position = position;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position >= 0){
if(data.length > position * 4){
holder.icon1.setImageURI(Uri.fromFile(data[position * 4]));
}
if(data.length > position * 4 + 1){
holder.icon2.setImageURI(Uri.fromFile(data[position * 4 + 1]));
}
if(data.length > position * 4 + 2){
holder.icon3.setImageURI(Uri.fromFile(data[position * 4 + 2]));
}
if(data.length > position * 4 + 3){
holder.icon4.setImageURI(Uri.fromFile(data[position * 4 + 3]));
}
}
return convertView;
}
}
用的AsyncTask
public class LazyAdapter extends BaseAdapter {
private Activity activity;
private File[] data;
private LayoutInflater inflater = null;
public LazyAdapter(Activity a, File[] files) {
activity = a;
data=files;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return data.length/4 + 1;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.gallery_item, null);
holder = new ViewHolder();
holder.icon1 = (ImageView) convertView.findViewById(R.id.imageView1);
holder.icon2 = (ImageView) convertView.findViewById(R.id.imageView2);
holder.icon3 = (ImageView) convertView.findViewById(R.id.imageView3);
holder.icon4 = (ImageView) convertView.findViewById(R.id.imageView4);
holder.position = position;
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(position >= 0){
new AsyncTask<ViewHolder, Void, Void>(){
@Override
protected Void doInBackground(ViewHolder... v) {
v[0].icon1.setImageURI(Uri.fromFile(data[v[0].position * 4]));
v[0].icon2.setImageURI(Uri.fromFile(data[v[0].position * 4 + 1]));
v[0].icon3.setImageURI(Uri.fromFile(data[v[0].position * 4 + 2]));
v[0].icon4.setImageURI(Uri.fromFile(data[v[0].position * 4 + 3]));
LazyAdapter.this.notifyDataSetChanged();
return null;
}
}.execute(holder);
}
return convertView;
}
}
我建議你使用https://github.com/nostra13/Android-Universal-Image-Loader圖像下載,如果可能的話,當然。這個庫將關注圖像加載,緩存和放置到您的視圖。它在列表視圖中非常出色。 –
您可以簡單地使用'Universal-ImageLoader'類。 – Piyush
這實際上並不是下載圖像。在這裏,只顯示'圖片'文件夾中的圖像 – enadun