我正在尝试在列表视图中显示图像。在一个单元格中,我一次显示4个图像。当我没有使用AsynkTask时,它可以工作,但它不是平滑滚动。所以我正在尝试使用Asynktasks。但在我的实现中,它没有显示任何内容。
没有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;
}
}
答案 0 :(得分:0)
你可以很容易地实现这种类型的列表......你会像这样使用...... 这里也使用了asynctask,但在这里我下载并将该图像存储在缓存内存所以它给出了平滑的滚动你会尝试这样....
公共类GalleryAdapter扩展了BaseAdapter {
private String[] mImagePaths;
private Activity mActivity;
Context mContext;
LayoutInflater mInflater;
public static LruCache<Integer, Bitmap> sGalleryBitmap;
public static int sCheckValue = 0;
public static int sCheckingFlags[] = null;
public GalleryAdapter(Activity a, String[] mGalleryString) {
super();
this.mActivity = a;
this.mImagePaths = mGalleryString;
this.mContext = (Context) mActivity.getBaseContext();
final int cacheSize = 20*1024*1024;
sGalleryBitmap = new LruCache<Integer, Bitmap>(cacheSize){
@Override
protected int sizeOf(Integer key, Bitmap bitmap) {
return bitmap.getByteCount();
}
};
mInflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
GalleryAdapter.sCheckingFlags = null;
GalleryAdapter.sCheckingFlags = new int[mImagePaths.length];
sCheckValue = 0;
}
@Override
public int getCount() {
return mImagePaths.length;
}
@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
convertView = null;
int n = 1;
convertView = mInflater.inflate(R.layout.gallery_support, null);
holder = new ViewHolder();
holder.imgDisplay = (ImageView) convertView
.findViewById(R.id.galleryimage);
if (ViewerActivity.sResource == position) {
holder.imgDisplay.setBackgroundResource(R.drawable.redback);
n = 0;
}
holder.bar = (ProgressBar) convertView.findViewById(R.id.bar);
convertView.setTag(holder);
if (getBitmapFromMemCacheGallery(position)== null) {
load o1 = new load(position, holder.imgDisplay, holder.bar);
o1.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,
mImagePaths[position]);
if (position == 0) {
holder.imgDisplay.setBackgroundResource(R.drawable.redback);
}
} else {
if (GalleryAdapter.sCheckingFlags[position] == 1) {
holder.imgDisplay.setImageBitmap(getBitmapFromMemCacheGallery(position));
} else {
holder.imgDisplay.setImageResource(R.drawable.galleryalart);
}
}
if (n == 1) {
convertView.setLayoutParams(new Gallery.LayoutParams(140, 140));
} else {
convertView.setLayoutParams(new Gallery.LayoutParams(140, 140));
}
return convertView;
}
public class ViewHolder {
ImageView imgDisplay;
ProgressBar bar;
}
private class load extends AsyncTask<String, Void, Bitmap> {
ImageView imageView;
ProgressBar bar;
int position;
public load(int position, ImageView imageView, ProgressBar bar) {
this.imageView = imageView;
this.bar = bar;
this.position = position;
this.imageView.setVisibility(View.GONE);
this.bar.setVisibility(View.VISIBLE);
}
@Override
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon = null;
try {
URLConnection connection;
connection = new java.net.URL(urldisplay).openConnection();
connection.setConnectTimeout(5000);
connection.setReadTimeout(5000);
InputStream in = connection.getInputStream();
mIcon = BitmapFactory.decodeStream(in);//ViewerActivity.decodeSampledBitmapFromResource(in, 140,140);
if(mIcon!=null)
{
addBitmapToMemoryCacheGallery(position, mIcon);
}
in.close();
} catch (Exception e) {
mIcon = null;
e.printStackTrace();
}
return mIcon;
}
@Override
protected void onPostExecute(Bitmap result) {
if (result != null) {
imageView.setImageBitmap(result);
imageView.setLayoutParams(new RelativeLayout.LayoutParams(140,
140));
imageView.setVisibility(View.VISIBLE);
bar.setVisibility(View.GONE);
GalleryAdapter.sCheckingFlags[position] = 1;
} else {
imageView.setBackgroundResource(R.drawable.galleryalart);
GalleryAdapter.sCheckingFlags[position] = 0;
imageView.setVisibility(View.VISIBLE);
bar.setVisibility(View.GONE);
Drawable myDrawable = mActivity.getResources().getDrawable(
R.drawable.galleryalart);
Bitmap bitmap = ((BitmapDrawable) myDrawable).getBitmap();
addBitmapToMemoryCacheGallery(position, bitmap);
}
}
}
@SuppressLint("NewApi")
public static void addBitmapToMemoryCacheGallery(Integer position, Bitmap bitmap) {
if (getBitmapFromMemCacheGallery(position) == null) {
sGalleryBitmap.put(position, bitmap);
}
}
@SuppressLint("NewApi")
public static Bitmap getBitmapFromMemCacheGallery(Integer imageKey) {
return sGalleryBitmap.get(imageKey);
}
}